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;
}
}
}
}
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
Post a Comment