Play Games

Search This Blog

Tuesday, July 14, 2015

RollUpSummary Exampe Trigger

In the sample code below, we are updating sum field(custom field) of account with sum of opportunities(amount field of the opportunity is to be sumed) related to that particular account

Sample Trigger Code :
trigger RollUpExample on opportunity (after insert,after update,after delete) {
   if(trigger.isInsert) {
    Set<id> accountIdSet = new Set<id>();
    Map<object,decimal> accountWithOppSumMap = new Map<object,decimal>();
    List<account> accountList = new List<account>();
      for (opportunity opp: Trigger.new)
        {
            if(opp.accountid!= null)
            {
                accountIdSet.add(opp.accountid);
                system.debug('ksk'+accountIdSet);
            }
        }
    AggregateResult[] groupedResults  = [SELECT SUM(amount) aver FROM opportunity where accountid in :accountIdSet];

    for (AggregateResult ar : groupedResults) {
        accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        system.debug('ksk'+accountWithOppSumMap);
    }
    for(account acc :[select id,sum__c from account where id in :accountIdSet]) {
        acc.sum__c = string.valueOf(accountWithOppSumMap.get(accountIdSet));
        accountList.add(acc);
    }
    update accountList;
   }
 
    if(trigger.isUpdate) {
    Set<id> accountIdSet = new Set<id>();
    Map<object,integer> accountWithOppSumMap = new Map<object,integer>();
    List<account> accountList  = new List<account>();
    for (opportunity opp: Trigger.new) {
        if(opp.accountid != null) {
            accountIdSet.add(opp.accountid);
        }
    }
    AggregateResult[] groupedResults  = [SELECT SUM(amount )aver FROM opportunity where accountid in :accountIdSet];

    for (AggregateResult ar : groupedResults) {
        accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        system.debug('ksk'+accountWithOppSumMap);
    }
    for(account acc  :[select id,sum__c from account where id in :accountIdSet]) {
        acc.sum__c= string.valueof(accountWithOppSumMap.get(accountIdSet));
        accountList.add(acc);
    }
     update accountList;
    }
 
    if(trigger.isDelete) {
        Set<id> accountIdSet = new Set<id>();
        Map<object,decimal> accountWithOppSumMap = new Map<object,decimal>();
        List<account> accountList = new List<account>();
        for (opportunity opp: Trigger.old) {
            if(opp.accountid != null)
            {
                accountIdSet.add(opp.accountid);
               // system.debug('ksk'+accountIdSet);
            }
        }
        AggregateResult[] groupedResultaccountIdSet  = [SELECT SUM(amount)aver FROM opportunity where accountid in :accountIdSet];
        for (AggregateResult ar : groupedResultaccountIdSet)
        {
            accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        }
        for(account acc  :[select id,sum__c from account where id in :accountIdSet])
        {
            acc.sum__c = string.valueof(accountWithOppSumMap.get(accountIdSet));
            accountList.add(acc );
        }
        update  accountList;
    }
}