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
anji punyamanthulaanji punyamanthula 

I want to sum of all the opprtunities amounts and display on Account object how to do that can any help me in that

I tried below code but i'm not getting o/p
Apex trigger:

trigger Rollup_Acc on Opportunity (After insert,After update,After delete,after undelete) {
    if(trigger.isinsert && trigger.isupdate && trigger.isdelete && trigger.isundelete){
    List<Account> accs=new List<Account>();
    Set<Id> ids=new Set<Id>();
    for(opportunity o:trigger.new){
        ids.add(o.AccountId);
        
    }
    decimal sum;
    for(Account acc :[select id,name,Amount__c,(select id,name,amount from opportunities ) from Account where id =:ids]){
        sum=0;
        for(opportunity op:acc.opportunities){
            sum+=op.Amount;
        }
        acc.AnnualRevenue=sum;
        accs.add(acc);
        
        
    }
   
    update accs;
    }
   
    
}
Best Answer chosen by anji punyamanthula
anji punyamanthulaanji punyamanthula
Thanks Varaprasad it's working fine

All Answers

Arpit Jain7Arpit Jain7
Hello Anji,

No need to write trigger for this.

Just create one roll up summary field on Account and select Summarized object as Opportunity then select Roll-Up type as "SUM" and select field to agreegate as "Amount" and you should be good to GO !!

Let me know for any issues.

Thanks
Arpit

Please mark this answer as SOLVED and BEST ANSWER if it helps you.
YogeshMoreYogeshMore
Hi Anji,

Best way to implement this by rollup summary field.

There is small mistake in your trigger code.
just change one thing from && to || it will like this.
 
if(trigger.isinsert || trigger.isupdate || trigger.isdelete || trigger.isundelete){

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44
v varaprasadv varaprasad
Hi Anji,

Please check below code once : 
 
trigger RollupSummaryTrigger on opportunity(after insert, after update, after delete, after undelete) {  
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        SampleRollupSummary.rollupOppAmounts(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        SampleRollupSummary.rollupOppAmounts(trigger.old);
    }
}


public class SampleRollupSummary {
    
    public static void rollupOppAmounts(list<opportunity> lstOfopps){
        system.debug('==lstOfopps== : '+lstOfopps);
        set<id> accIds = new set<id>();
        list<account> updLstOfAccs = new list<account>();
        list<opportunity> lstCons = new list<opportunity>();
        double totalAmount = 0;
        for(opportunity opp : lstOfopps){
            accIds.add(opp.accountid);
        }
        system.debug('==accIds==:'+accIds);
        list<account> lstAccs = [select id,name,Total_Count__c, (select id,amount from opportunities) from account where id in : accIds];
        
        for(account acc : lstAccs){
            for(opportunity op : acc.opportunities){
                totalAmount = totalAmount + op.amount;
                
            }
            acc.Total_Count__c =  totalAmount; 
            updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
        
        
    }
    
}

Hope it helps you.
Please mark it as best answer if the above information is informative.
In case of any other assistance please let me know.

Thanks,
Varaprasad 
Salesforce Developer
more:  varaprasad4sfdc@gmail.com

 
anji punyamanthulaanji punyamanthula
Even though its not working its getting null pointer exception
YogeshMoreYogeshMore
Hi Anji,

Try following code.
 
trigger RollupSummaryTrigger on opportunity(after insert, after update, after delete, after undelete) {  
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        SampleRollupSummary.rollupOppAmounts(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        SampleRollupSummary.rollupOppAmounts(trigger.old);
    }
}


public class SampleRollupSummary {
    
    public static void rollupOppAmounts(list<opportunity> lstOfopps){       
        set<id> accIds = new set<id>();        
		list<account> updLstOfAccs = new list<account>();
                
		for(opportunity opp : lstOfopps){
            accIds.add(opp.accountid);
        }		
        
        list<account> lstAccs = [select id,name,Total_Count__c, (select id,amount from opportunities) from account where id in : accIds];
        
        for(account acc : lstAccs){
            double totalAmount = 0;
			for(opportunity op : acc.opportunities){
				If(op.amount > 0){
					totalAmount = totalAmount + op.amount;
				}
            }
            acc.Total_Count__c =  totalAmount; 
            updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
    }    
}

let us know if it helps you.

Regards,
Yogesh
mukesh guptamukesh gupta
Hi Anji,

I have tested my code it's working 100% correct , Please use
 
trigger GrossAmountRollUp on Opportunity (after insert, after update) {
    List<Account> accList = new List<Account>();
    List<Id> listIds = new List<Id>();
    Decimal grossTotal = 0;
    
    for(Opportunity opp: Trigger.new){
        listIds.add(opp.AccountId);
    }
    
 for(Account a: [select Name,(select id, Amount from opportunities) from Account  where id =:listIds])
    {   
       for(Opportunity o:a.opportunities){
          grossTotal  = grossTotal+o.Amount;
        }
        a.Gross_Amount__c = grossTotal;
        accList.add(a);
            
    }
    
    update accList;
}


Please Mark as Best Answer!!!!!

Regards
Mukesh
anji punyamanthulaanji punyamanthula
Hi mukesh thanks for your support its working fine but my thing is its working on insert and update its not working on delete and undelete operation i tried but i'm not getting well.let me know whether u help in that or not
v varaprasadv varaprasad
Hi Anji,

Plz check once below code : 

 
trigger RollupSummaryTrigger on opportunity(after insert, after update, after delete, after undelete) {  
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        SampleRollupSummary.rollupOppAmounts(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        SampleRollupSummary.rollupOppAmounts(trigger.old);
    }
}


public class SampleRollupSummary {
    
    public static void rollupOppAmounts(list<opportunity> lstOfopps){
        system.debug('==lstOfopps== : '+lstOfopps);
        set<id> accIds = new set<id>();
        list<account> updLstOfAccs = new list<account>();
        list<opportunity> lstCons = new list<opportunity>();
        double totalAmount = 0;
        for(opportunity opp : lstOfopps){
            accIds.add(opp.accountid);
        }
        system.debug('==accIds==:'+accIds);
        list<account> lstAccs = [select id,name,Total_Count__c, (select id,amount from opportunities) from account where id in : accIds];
        
        for(account acc : lstAccs){
            for(opportunity op : acc.opportunities){
			   If(op.amount > 0){
                totalAmount = totalAmount + op.amount;
                }
            }
            acc.Total_Count__c =  totalAmount; 
            updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
        
        
    }
    
}

Thanks
Varaprasad
anji punyamanthulaanji punyamanthula
Thanks Varaprasad it's working fine
This was selected as the best answer
mukesh guptamukesh gupta
Hi Anji,

Please use this code, it's working 100% correct:-
 
trigger GrossAmountRollUp on Opportunity (after insert, after update, after delete, after undelete) {
  	
  if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
  		GrossAmountClass.totalOppAmount(trigger.new);
  }else if(trigger.isAfter && trigger.isDelete){
        GrossAmountClass.totalOppAmount(trigger.old);
  }

}
 
public class GrossAmountClass {
	public static void totalOppAmount(List<Opportunity> opp){
        List<Account> accList = new List<Account>();
        List<Id> listIds = new List<Id>();
        Decimal grossTotal = 0;
        listIds.add(opp[0].AccountId);
    	
 for(Account a: [select Name,(select id, Amount from opportunities) from Account  where id =:listIds])
    {   
       for(Opportunity o:a.opportunities){
          grossTotal  = grossTotal+o.Amount;
        }
        a.Gross_Amount__c = grossTotal;
        accList.add(a);
    }
    	update accList;
    }
}

Please MARK AS A BEST ANSWER!!!!

Regards
Mukesh