onAccountCalculateTotalAssestPriceOnUpdateAccount on Account

trigger onAccountCalculateTotalAssestPriceOnUpdateAccount on Account (before update) {
   

  Set<Id>accId = new Set<Id>();
  List<Asset> lstass = new List<Asset>();
  Map<Id,List<Asset>> mapAss = new Map<id,List<Asset>>();
 
 
  for(Account acc:trigger.new)
  {
    accId.add(acc.id);
  }
 
  lstAss= [Select price ,id, accountId from Asset where accountId In:accId];

  if(lstAss.size()>0&&lstAss!=null)
  {
  for(Asset assObj:lstAss)
  {
    if(mapAss.get(assObj.AccountId)==null)
    {
        mapAss.put(assObj.AccountId, new List<Asset>());
    }
    mapAss.get(assObj.AccountId).add(assObj);
   
  }
 
  for(Account accObj:trigger.new)
  {
 
  if(mapAss !=null && mapAss.get(accObj.Id)!=null)
  {
   List<Asset> lstasset = new List<Asset>();
   Decimal total=0;
   lstasset = mapAss.get(accObj.id);
 
   for(Asset assObj:lstasset)
   {
       if(assobj!=null && assObj.price!=null)
       {
     
       total = total+assObj.price;
     
       }
     
   }
 
   accObj.Total_Assets_Price__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