+ Start a Discussion
Sreenivas VSreenivas V 

trigger to rollup opportunity amount on Account field where opportunity record type is "Revenue Type" Opportunity field: Opty_Amount_C Account field: Total_Opty_Amount_C

Hi,
   I got this question in some interview, where I tried to solve but unsuccessful. Please help me out.

trigger to rollup opportunity amount on Account field where opportunity
record type is "Revenue Type"
Opportunity field: Opty_Amount_C
Account field: Total_Opty_Amount_C
Best Answer chosen by Sreenivas V
Pradeep SinghPradeep Singh
Hi,
Please refer below code
trigger rollupopp on Opportunity (after delete,after update,after insert,after undelete) {
    
    set<ID>AccIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(opportunity opp : trigger.new){
            AccIds.add(opp.AccountId);
        }
    }
    if(trigger.isdelete){
        for(opportunity opp : trigger.old){
            AccIds.add(opp.AccountId);           
        }        
    }
       
    if(trigger.isupdate){
        for(opportunity opp:trigger.new){
            AccIds.add(opp.AccountId);
            if(trigger.oldmap.get(opp.id).AccountId != opp.AccountId && trigger.oldmap.get(opp.id).AccountId != null ){
                AccIds.add(trigger.oldmap.get(opp.id).AccountId);
            }            
        }
    }    
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [select AccountId ,SUM(Opty_Amount_C) SOA,count(id) cc from opportunity where AccountId in:AccIds AND RecordType.Name = 'Revenue Type' group by AccountId]){
        amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('SOA')));
       // amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('cc')));
    }
    list<account>acclist = new list<account>();
   
    for(id iid : AccIds){
        account acnt = new account(id=iid);
        if(amtmap.containskey(iid)){
            acnt.Total_Opty_Amount_C = amtmap.get(iid);
        }else{
            acnt.Total_Opty_Amount_C = 0;
        } 
        acclist.add(acnt);       
    }
   
    if(acclist.size()>0){
        update acclist;
    }
   
}

If this helps, please mark it as best answer.

All Answers

Pradeep SinghPradeep Singh
Hi,
Please refer below code
trigger rollupopp on Opportunity (after delete,after update,after insert,after undelete) {
    
    set<ID>AccIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(opportunity opp : trigger.new){
            AccIds.add(opp.AccountId);
        }
    }
    if(trigger.isdelete){
        for(opportunity opp : trigger.old){
            AccIds.add(opp.AccountId);           
        }        
    }
       
    if(trigger.isupdate){
        for(opportunity opp:trigger.new){
            AccIds.add(opp.AccountId);
            if(trigger.oldmap.get(opp.id).AccountId != opp.AccountId && trigger.oldmap.get(opp.id).AccountId != null ){
                AccIds.add(trigger.oldmap.get(opp.id).AccountId);
            }            
        }
    }    
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [select AccountId ,SUM(Opty_Amount_C) SOA,count(id) cc from opportunity where AccountId in:AccIds AND RecordType.Name = 'Revenue Type' group by AccountId]){
        amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('SOA')));
       // amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('cc')));
    }
    list<account>acclist = new list<account>();
   
    for(id iid : AccIds){
        account acnt = new account(id=iid);
        if(amtmap.containskey(iid)){
            acnt.Total_Opty_Amount_C = amtmap.get(iid);
        }else{
            acnt.Total_Opty_Amount_C = 0;
        } 
        acclist.add(acnt);       
    }
   
    if(acclist.size()>0){
        update acclist;
    }
   
}

If this helps, please mark it as best answer.
This was selected as the best answer
Rahul Jain 169Rahul Jain 169
User-added image

This is how you can roll up a field of Opportunity based on a Record Type.

Please Mark this as best answer if it answers your question.
Sreenivas VSreenivas V
@Rahul,
     I wanted a apex code for the above problem, indeed thanks for your suggestion.
Jithesh VasudevanJithesh Vasudevan
Hi Sreenivas,

trigger AmountTrigger on Opportunity (after update,after insert) 
{
Set<id> accid= new Set<id>();
List<Account> ac1= new List<Account>();


for(Opportunity op3:Trigger.new)
{
accid.add(op3.AccountId);
}

List<Account> ac2=[select id,Amount__c,(select id,Amount from Opportunities where RecordTypeId='<recordtypeId>') from Account where ID =:accid];

for(account opn:ac2)
{
Double TotAmt=0.0;
 for(Opportunity op4:opn.opportunities)
 {
 
TotAmt+=op4.Amount;

}
opn.Amount__c=TotAmt;
ac1.add(opn);

}
Update ac1;


}


Do not forget to give the recordtypeId which I have highlighted with a bold letters. This will work.
Sreenivas VSreenivas V
@Jithesh,
   Thanks for the code. even pradeep singh's code also works and covered all scenarios.
Jithesh VasudevanJithesh Vasudevan
Sreenivas,

Great..Good luck for your job interviews..