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
pranavshahpranavshah 

Apex trigger for opportunity object

Hi All, 
 their is one requirement i have ..
i have 'Amount field' on opportunity needs to be entered whenever a opportunity is created related to a particular account  once a opportunity is created ,then i want 'Total Opportunity Amount' field on Account object needs to be updated.

I want to write trigger for the same

How it will be done

I have written this much amount of code, but i am not able to go ahead..as i am on learning stage ..please help me out.

public class opportunityhandler
{
 public void opportunityamount(list<opportunity> newopportunity)
 {
  set<String> setOppName=new set<String>();
  for(opportunity opp:newopportunity)
  {
    setOppname.add(opp.Amount);
  }
    list<opportunity> opps= [select Name, Amount from opportunity where AccountId =:setOppname];
    {
    
    }
  }
 }
 
v varaprasadv varaprasad
Hi Pranav,

Please check once following code. use your field Total Opportunity Amount instead of Total_Count__c .
 
==========Trigger==============
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);
    }
}

==============class===========
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 != null)
                totalAmount = totalAmount + op.amount;
                
            }
            acc.Total_Count__c =  totalAmount; 
            updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
        
        
    }
    
}
========================================================




Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 
Prashant Pandey07Prashant Pandey07
Hi Pavan,

Since you are at earning state I would like to recommend the use of Map and avoid nested for loop. @varaprasad code will work perfectly but we can improve more in term of performance.
 
<!--------------------Class------------------------->

public class SampleRollupSummary {

public class SampleRollupSummary {

public static void rollupOppAmounts(Map<id,opportunity> newmap){
double totalAmount = 0;

set<id>ids=new set<id>();

for(Opportunity op:newmap.values()){
ids.add(op.accountid);
}
map<id,Account> mapofacc=new map<id,account>([select id,(select id,amount from opportunities) from account where id=:ids]);
map<id,account> mapacc=new map<id,account>();

Map<id,opportunity> mapofopp=new map<id,opportunity>([select id,accountid,amount from opportunity where accountid=:ids]);

for(opportunity op:mapofopp.values()){
     if(op.amount!=null){
     totalAmount = totalAmount + op.amount;
     if(mapofacc.containsKey(op.accountid)){
     mapofacc.get(op.accountid).age__c=totalAmount;
     mapacc.put(op.accountid,mapofacc.get(op.accountid));
          }
        }
     }
if(mapacc.size()>0){
update mapacc.values();
}
    }


<!--------------------Trigger------------------------->


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

    }

}

Please mark this as the best answer and help the community grow...

--
Thanks,
Prashant
 
pranavshahpranavshah
Hey Varaprasad, Will you explain me line by line for class...