function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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..
Sai Kumar Reddy. TSai Kumar Reddy. T
@Jithesh vasudevan could you please help me to write a trigger for this
Create a field on Account. field name would be Total Opportunity Amount(data type – Currency(16,2)). When an Opportunity is inserted/updated then the  Total Opportunity Amount will be updated on associated Account from All Opportunities related to this account.
 
thanks in Advance