[Salesforce]Apex コントローラでの内部クラスの使用

Apex コントローラでの内部クラスの使用

public with sharing class SampleController {

    //Sample用
    public SampleSetting Sample {get;set;}

    // コンストラクタ
    public SampleController(){
        //初期処理
        init();
    }

    //初期処理
    private void init(){
        //Sample
        SampleSet();
    }


    //Sample
    private void SampleSet(){

        // カスタム[SampleSetting]から情報取得
        SampleSetting__c mc = SampleSetting__c.getOrgDefaults();
        mc = SampleSetting__c.getOrgDefaults();

        this.Sample =  new SampleSetting();
        this.Sample.Sample_Key = mc.Sample_Key__c;
    }

    //Sampleクラス
    public class SampleSetting {

        public String Sample_Key {get;set;}

        public SampleSetting(){
            this.Sample_Key = '';
        }
            
    }

}

[Salesforce]Apexトリガのアップデートの前後の値の比較

Apexトリガのアップデートの前後の値の比較

trigger UpdateProductObjTrigger on ProductObj__c (before update) {
    for(ProductObj__c productObj : Trigger.New) {
        ProductObj__c oldProductObj = Trigger.oldMap.get(productObj.id);
        if (productObj.Status__c != oldProductObj.Status__c ) {
            productObj.Is_Updated__c = true;
        }
    }
}

[Salesforce]Apexバッチで集計関数

Apexバッチで集計関数

//バッチクラス
global class SampleAggregateBatch implements Database.Batchable<AggregateResult> {
  // The batch job starts
  global Iterable<AggregateResult> start(Database.BatchableContext bc){
    String query = 'SELECT COUNT(Id) cnt, AccountId FROM Contact GROUP BY AccountId';
    return new SearchResultIterable(query);
  } 
  // The batch job executes and operates on one batch of records
  global void execute(Database.BatchableContext bc, List<sObject> scope){ 
    for(sObject sObj : scope) {
      AggregateResult ar = (AggregateResult)sObj;
      System.debug('>>>> COUNT : ' + ar.get('cnt'));
    }
  }
  // The batch job finishes
  global void finish(Database.BatchableContext bc){ }
}

//インタフェースIterable Apex class
public class SearchResultIterable implements Iterable<AggregateResult>{
	private String query;

    public SearchResultIterable(String soql){
    	this.query = soql;
  	}

    public Iterator<AggregateResult> Iterator(){
        return new SearchResultIterator(this.query);
    }
}

//インタフェースIterator
public class SearchResultIterator Implements Iterator<AggregateResult>{
    public AggregateResult [] results {get;set;}
    public Integer index {get;set;}

    public SearchResultIterator(String query){
        this.index = 0;
        results = Database.query(query);
    }

    public boolean hasNext(){
        return results !=null && !results.isEmpty() && index < results.size();
    }

    public AggregateResult next(){
        return results[index++];
    }
}

// Run batch apex
SampleAggregateBatch batch = new SampleAggregateBatch();
Database.executebatch(batch, 200);

// Debug log output
>>>> COUNT : 1
>>>> COUNT : 3
>>>> COUNT : 2

[Salesforce]SOQL文のHAVINGの使い方

SOQL文のHAVING

HAVINGではGROUP BYでグルーピングした結果に対して、絞込みの条件を入れることが出来ます。

絞込み条件という意味ではWHEREと似ていますが、WHEREではグルーピングする前、HAVINGはグルーピングした後です。

次のサンプルは商談のリードソースでグルーピングし、同じ値のレコード数が1以上のリードソース別金額を表示します。

AggregateResult[] results = [SELECT LeadSource, SUM(Amount) summary 
                               FROM Opportunity 
                               GROUP BY LeadSource 
                               HAVING Count(LeadSource) > 1];
for(AggregateResult ar: results){
  System.debug('LeadSource='+ ar.get('LeadSource') 
                   + ':Amount='+ ar.get('summary'));
}

デバッグの結果

LeadSource=LeadSource1:Amount=2
LeadSource=LeadSource2:Amount=3