onAccountAddTotalAmountOfOppAmount on Account

trigger onAccountAddTotalAmountOfOppAmount on Account (before update) {
    if(clsForStatusofType.thirdrun == false)
    {
         clsForStatusofType.thirdrun=true;
    }
    else
    {
        return;
    }
    Set<Id>accid = new Set<Id>();
    map<Id,List<Opportunity>> mapopp= new map<Id,List<Opportunity>>();
   
    List<Opportunity> lstopp = new List<Opportunity>();
    List<Account>lstacc = new List<Account>();
    for(Account acc:trigger.new)
    {
         accid.add(acc.id);
    }
   
    lstopp =[Select Amount,Id,AccountId from Opportunity where AccountId In:accid];
    if(lstopp.size()>0 && lstopp!=null)
    {
    for(Opportunity opp:lstopp)
    {
         if(mapopp.get(opp.AccountID)==null)
        {
            mapopp.put(opp.AccountID,new List<Opportunity>());
        }
        mapopp.get(opp.AccountId).add(opp);
    }
  for(Account accObj:trigger.new)
  {
   
      List<Opportunity> lstopprs = new List<Opportunity>();

        if(mapopp != null && mapopp.get(accObj.Id) != null){
          lstopprs = mapopp.get(accObj.Id);
          system.debug('HHHH '+lstopprs);
      }
      Decimal total = 0;
      if( lstopprs !=null && lstopprs.size()>0)
      {
      for(Opportunity oppObj:lstopprs)
      {
        if(oppObj.Amount != null)
        {
           total = total+oppObj.Amount;
        }
      }
      }
      system.debug('KKK '+total);
      accObj.Total_Opportunity_Amount__c = total;
     
   
  }
    }


}

Comments

Popular Post

Write The Dynamic Query in Salesforce and bind in page block table with filter conditions

Fetch Picklist value in LWC using Schema class

Custom Section Color at Visual force page