+ Start a Discussion
RSKRSK 

How to update assets of a merged account

Hi,

I have a trigger on Asset to update no. of  assests of an Account on a account  custom field, it is working as expcted but failing to update assets when  merge accounts.  Any help must be highly appreciated.

 

Here is my trigger:

trigger updateAssetCountonAccount on Asset(after insert,after update,after delete) {
List<Account> updateAssetCountList = new List<Account>();
List<Asset> assId = new List<Asset>();
Map<Id,Integer> mapAccount = new Map<Id,Integer>();
set<Id> accountIds = new set<Id>();
for(Asset a : Trigger.new){
      accountIds.add(a.AccountId);
}

mapAccount = foundAsset(accountIds);
updateAssetCountList = [select asset_count__c,id from Account where Id in : mapAccount.KeySet()];
for(Account a: updateAssetCountList)
{
    a.asset_count__c = mapAccount.get(a.Id);
}

update updateAssetCountList;

public Map<Id,Integer> foundAsset( set<Id> AccountIds2){

assId = [select id,AccountId from Asset where AccountId in:accountIds2];
for(Asset accId : assId)
{
    if(mapAccount.containsKey(accId.AccountId))
    {
        integer sum;
        sum = mapAccount.get(accId.AccountId);
        sum = sum+1;
        mapAccount.put(accId.AccountId,sum);
    }
    else
    {
        integer sum;
        sum = 1;
        mapAccount.put(accId.AccountId,sum);
    }
 }


 return mapAccount;
}
}

 

Thanks  in advance !

 

Regards,

Rsk

kiranmutturukiranmutturu

when ever merge statement is using the reslut fires for only before update triggers i.e. 

before update triggers for the winning record only...hope you got my point

RSKRSK

Thank you for your response.

 

I have tried with before update trigger but no luck,  its failed to update total assets on account when merging accounts

 

 

Regards,

Rsk