[Salesforce]Apexトリガのガバナ制限について

SalesforceのApexトリガでは、ガバナ制限することができません。

対応策としては、データローダでバッチサイズを調整してガバナ制限を対応する。

バッチサイズは、1トランザクションでのガバナ制限を超えないところに、設定する。

[Salesforce]Visualforceのキャンセルボタンの必須チェック不要処理

Visualforceでinputfieldでキャンセルボタン時には必須を適用させたくない場合に、以下の例で対応する。

<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>

ポイントは、「immediate=”true”」パラメータで対応可能となります。

上記のパラメータがないと、キャンセルボタン押下時にも必須チェックが行われる。

[Salesforce]月末日計算ロジック

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]デバッグログ一括削除

Salesforceのデバッグログを一括削除する手順を共有します。

1.開発コンソルを開く

2.Query Editorにて、以下のSQL文を実行する。

select Id from ApexLog

3.Query Resultsに出力されたデバッグログIDを全選択して、「Delete Row」ボタン押下する。

そのほかの削除方法は以下です。

データローダで「デバッグログ」をExportしてDeleteする。

[Salesforce]カスタム設定の設定種別をリスト選択肢表示

カスタム設定画面にて設定種別の選択肢としてリストを表示するための設定は以下です。

設定 > 検索 > スキーマ

検索結果で、スキーマ設定にて、
リストカスタム設定の種別を管理を有効化します。

[Salesforce]ランダム文字列生成メッソド

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]Apex RESTコールアウト

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);
    }
}