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 posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page