You need to sign in to do that
Don't have an account?
Trigger help
Hi, I'm trying to update a custom field asset_count__c which counts no of assets of an account, when updating account and getting the following error. Any help must me highly appreciated.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updateAssetCounton caused an unexpected exception, contact your administrator: updateAssetCounton: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00190000004K1vLAAS; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00190000004K1vL) is currently in trigger updateAssetCounton, therefore it cannot recursively update itself: []: Trigger.updateAssetCounton: line 20, column 1
trigger updateAssetCounton on Account (before update) {
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(Account acc : Trigger.new){
accountIds.add(acc.Id);
}
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; // line 20
public Map<Id,Integer> foundAsset( set<Id> AccountIds2){
assId = [select id,AccountId from Asset where AccountId in:accountIds2];
if(assId.size()>0)
{
for(Asset accId : assId)
{
if(mapAccount.containsKey(
{
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);
}
}
}
else
{
for(Id accId : accountIds2)
{
mapAccount.put(accId,0);
}
}
return mapAccount;
}
}
You might find this to be a simpler way to achieve the same result. You're getting the error since you're calling an update inside of an update trigger which starts an infinite recursive process.
trigger updateAssetCount on Account ( before update ) {
Map<Id,integer> assetCount = new Map<Id,integer>(); // map the account id to total asset count
for( Account a : [ select Id, ( select Id from Assets ) from Account where Id IN :Trigger.New ] ) { // for each Account, loop through the child Assets and increment count
integer count = 0;
for( Asset asset : a.Assets ) {
count++;
}
assetCount.put( a.Id, count ); // map the Account Id to the total number of Assets.
}
for( Account account : Trigger.New ) { // assign the total Asset count to each Account.
account.Asset_Count__c = assetCount.get( account.Id ); // you don't need to call an update since your trigger is firing before update
}
}
Hope that helps!
Clint
All Answers
Hi,
You are getting this error because your trigger is recursively called. Your trigger in on update of account and you are updating the account at the end. So to overcome this you have to create a class and in that class define a static Boolean type variable with default value as false. Now make the condition inside the trigger on the basis of static variable.
For more details please go through the link below:
http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
You might find this to be a simpler way to achieve the same result. You're getting the error since you're calling an update inside of an update trigger which starts an infinite recursive process.
trigger updateAssetCount on Account ( before update ) {
Map<Id,integer> assetCount = new Map<Id,integer>(); // map the account id to total asset count
for( Account a : [ select Id, ( select Id from Assets ) from Account where Id IN :Trigger.New ] ) { // for each Account, loop through the child Assets and increment count
integer count = 0;
for( Asset asset : a.Assets ) {
count++;
}
assetCount.put( a.Id, count ); // map the Account Id to the total number of Assets.
}
for( Account account : Trigger.New ) { // assign the total Asset count to each Account.
account.Asset_Count__c = assetCount.get( account.Id ); // you don't need to call an update since your trigger is firing before update
}
}
Hope that helps!
Clint
Its working. Great !
Thank you very much for your help, really appreciate it.
Regards,
RSK
Hi Clint,
Thank you for your help.
Is it possible to query inner query based on condition ?, here i want to query assets where asset.Claimed_account__c (custom Account lookup filed) equal to current Account (Trigger.new). Is it possible ?
Query:
for( Account a : [ select Id, ( select Id,Claimed_Account__c from Assets where Claimed_Account__c IN :Trigger.New ) from Account where Id IN :Trigger.New ] ){
integer count = 0;
for( Asset asset : a.Assets ) {
count++;
}
I dont get any errors here but its not working as expected , is it a valid query ?.
Thanks in advance!
Regards,
RSK