SalesforceのApexトリガでは、ガバナ制限することができません。
対応策としては、データローダでバッチサイズを調整してガバナ制限を対応する。
バッチサイズは、1トランザクションでのガバナ制限を超えないところに、設定する。
kinkun's blog
SalesforceのApexトリガでは、ガバナ制限することができません。
対応策としては、データローダでバッチサイズを調整してガバナ制限を対応する。
バッチサイズは、1トランザクションでのガバナ制限を超えないところに、設定する。
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の四捨五入について共有します。
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、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);
}
Visualforceでinputfieldでキャンセルボタン時には必須を適用させたくない場合に、以下の例で対応する。
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
ポイントは、「immediate=”true”」パラメータで対応可能となります。
上記のパラメータがないと、キャンセルボタン押下時にも必須チェックが行われる。
Apexで月末日を計算するロジックを共有します。
date dateField = date.newInstance(1987, 12, 17);
Integer numberOfDays = Date.daysInMonth(dateField.year(), dateField.month());
Date lastDayOfMonth = Date.newInstance(dateField.year(), dateField.month(), numberOfDays);
system.debug('lastDayOfMonth:'+lastDayOfMonth);
Salesforceのデバッグログを一括削除する手順を共有します。
1.開発コンソルを開く
2.Query Editorにて、以下のSQL文を実行する。
select Id from ApexLog
3.Query Resultsに出力されたデバッグログIDを全選択して、「Delete Row」ボタン押下する。
そのほかの削除方法は以下です。
データローダで「デバッグログ」をExportしてDeleteする。
カスタム設定画面にて設定種別の選択肢としてリストを表示するための設定は以下です。
設定 > 検索 > スキーマ
検索結果で、スキーマ設定にて、
リストカスタム設定の種別を管理を有効化します。
Salesforceでランダム文字列生成メッソドを共有します。
public String getRandomString(integer LengthRequired){
String small = 'abcdefghijklmnopqrstuvwxyz';
String big = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
String num = '0123456789';
String max = small + big + num;
String result = '';
integer position;
List<String> random = new List<String> ();
// 英小文字から最低1文字選択
position = Integer.valueof(String.valueof(Math.roundToLong((small.length()-1)*Math.random())));
random.add(small.substring(position,position+1));
// 英大文字から最低1文字選択
position = Integer.valueof(String.valueof(Math.roundToLong((big.length()-1)*Math.random())));
random.add(big.substring(position,position+1));
// 数字から最低1文字選択
position = Integer.valueof(String.valueof(Math.roundToLong((num.length()-1)*Math.random())));
random.add(num.substring(position,position+1));
// 残りの文字を全文字を対象にランダムで選択
for(Integer i = 0; i < LengthRequired - 3; i++) {
position = Integer.valueof(String.valueof(Math.roundToLong((max.length()-1)*Math.random())));
random.add(max.substring(position,position+1));
}
// 文字列の並び回を最低回数繰り返す
for(Integer i=0; i< 100; i++) {
Integer bef = Integer.valueof(String.valueof(Math.roundToLong((random.size()-1)*Math.random())));
Integer aft = Integer.valueof(String.valueof(Math.roundToLong((random.size()-1)*Math.random())));
String tmp = random[bef];
random[bef] = random[aft];
random[aft] = tmp;
}
for(String s : random) {
result += s;
}
return result;
}
メッソド呼び出し
system.debug(getRandomString(6))
メッソド呼び出し結果
例えば、abcdef
Salesforceからコールアウトを実行して、外部サービスを受信する。
GETの例
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for (Object animal: animals) {
System.debug(animal);
}
}