[Salesforce]String.isBlankとisEmptyの動き
Salesforce のStringメッソドのisBlankとisEmptyについて例を共有します。
String emptyStr = '';
String blankStr = ' ';
String nullStr = null;
System.debug(String.isEmpty(emptyStr)); // true
System.debug(String.isEmpty(blankStr)); // false
System.debug(String.isEmpty(nullStr )); // true
System.debug(String.isBlank(emptyStr)); // true
System.debug(String.isBlank(blankStr)); // true
System.debug(String.isBlank(nullStr )); // true
[Japanese]泥臭い
泥臭い
どろ くさい
Engligh
Muddy
洗練されていず、やぼったい。スマートでない。田舎くさい。
粘り強く
着実に
[Salesforce]SOQLで子レコードを持つ親レコード取得
Salesforceで子レコードを持つ親レコードを取得するSOQL文を共有します。
[SELECT Id
FROM Oya__c
WHERE Id IN (SELECT Oya__c FROM Ko__c)
]
[Salesforce]親から子のレコード取得
Salesforceで親から子のレコードを取得するSOQL文を共有します。
targetList = [SELECT Id, (SELECT Id FROM Ko__r) FROM Oya__c ];
[Salesforce]Apexトリガの二重起動退避対策
SalesforceのApexトリガで、Updateの場合に、ワークフローによる該当オブジェクトの更新による、Apexトリガが二重で起動することがあります。
それによって、予期せぬ動きが発生する場合があります。
その防止対策として、Apexトリガのハンドラークラスに、static変数を使用して、初回起動かどうかをApexトリガが判断し、二重防止を回避する。
以下はサンプルです。
--- Apexトリガのハンドラクラス ---
public class ApexTriggerHandler {
public static boolean firstRun = true;
}
--- Apexトリガ ---
trigger ApexTestTrigger on TestObject__c (before update) {
if(p.firstRun==true){
system.debug('1回目の起動です。');
ApexTriggerHandler.firstRun=false;
}else{
system.debug('2回目の起動です。');
}
}
[Salesforce]Apexトリガのガバナ制限について
SalesforceのApexトリガでは、ガバナ制限することができません。
対応策としては、データローダでバッチサイズを調整してガバナ制限を対応する。
バッチサイズは、1トランザクションでのガバナ制限を超えないところに、設定する。
[Salesforce]切り捨て
Salesforceの切り捨てについて共有します。
Decimal[] example = new Decimal[]{5.5, 1.1, -1.1, -2.7};
Long[] expected = new Long[]{5, 1, -1, -3};
for(integer x = 0; x < example.size(); x++){
System.debug(Math.floor(example[x]));
}
結果
5
1
-2
-3
[Salesforce]四捨五入
Salesforceの四捨五入について共有します。
Decimal roundNumber = 10.4;
System.debug(roundNumber.setScale(0,RoundingMode.HALF_UP));
roundNumber = 10.5;
System.debug(roundNumber.setScale(0,RoundingMode.HALF_UP));
roundNumber = 10.6;
System.debug(roundNumber.setScale(0,RoundingMode.HALF_UP));
結果
10
11
11
[Salesforce]try、catch、ロールバック
salesforceのtry、cahch、ロールバックの例を共有します。
Savepoint sp = Database.setSavepoint();
try {
Merchandise__c m = new Merchandise__c();
insert m;
} catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
Database.rollback(sp);
}