function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Richa_LearningRicha_Learning 

Trigger Error

Hi,

 

Actually I already have a trigger that copy some fields from Account to IMP__c. When I deactivate the Account trigger this trigger works fine. But when Account trigger is active it gives me the below error:

 

(This below Trigger is copying status from IMP__C to Account )

 

 

trigger updatefield on imp__c (after update) {
Map<String,imp__c > recordsimp = new Map<String,imp__c >();
List<account> accountToUpdate = new List<account>();

for(imp__c impRecord : trigger.new){
recordsimp.put(impRecord.rec_ID__c,impRecord);
}

accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];
for(account accounts: accountToUpdate){
accounts.status__c = recordsimp.get(accounts.account_num__c).Status__c;
}
if(accountToUpdate.size() > 0){
update accountToUpdate;
}
}

 

 

 

 

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updatefield caused an unexpected exception, contact your administrator: updatefield: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 087Z00000008cAfIAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertfielddat: maximum trigger depth exceeded IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] IAccount trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf] IMP trigger event AfterUpdate for [a0SZ0000001cnpO] Account trigger event AfterUpdate for [087Z00000008cAf]: []: Trigger.updatefield: line 18, column 1

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen

Your two triggers are firing off each other, so they are essentially stuck in an infinite loop, and you hit your governor limit of trigger stack depth. Try to add a couple of conditions to your triggers so that DML operation(s) only occur when the conditions are met. Below in red is how I would suggest you update your IMP__c trigger, and I would recommend making similar changes to your Account trigger as well.

 

trigger updatefield on imp__c (after update) {
Map<String,imp__c > recordsimp = new Map<String,imp__c >();
List<account> accountToUpdate = new List<account>();

for(imp__c impRecord : trigger.new){

Imp__c oldImp = trigger.oldMap.get(impRecord.Id);

// only add IMP record Ids to the Map if the IMP record Status__c field changes

if(impRecord.Status__c != oldImp.Status__c)
recordsimp.put(impRecord.rec_ID__c,impRecord);
}

 

// only execute below if the Map is not empty (any Status__c changes occured above)

if(!recordsimp.isEmpty(){
accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];
for(account accounts: accountToUpdate){
accounts.status__c = recordsimp.get(accounts.account_num__c).Status__c;
}
if(accountToUpdate.size() > 0){
update accountToUpdate;
}
}

}

All Answers

k_bentsenk_bentsen

Your two triggers are firing off each other, so they are essentially stuck in an infinite loop, and you hit your governor limit of trigger stack depth. Try to add a couple of conditions to your triggers so that DML operation(s) only occur when the conditions are met. Below in red is how I would suggest you update your IMP__c trigger, and I would recommend making similar changes to your Account trigger as well.

 

trigger updatefield on imp__c (after update) {
Map<String,imp__c > recordsimp = new Map<String,imp__c >();
List<account> accountToUpdate = new List<account>();

for(imp__c impRecord : trigger.new){

Imp__c oldImp = trigger.oldMap.get(impRecord.Id);

// only add IMP record Ids to the Map if the IMP record Status__c field changes

if(impRecord.Status__c != oldImp.Status__c)
recordsimp.put(impRecord.rec_ID__c,impRecord);
}

 

// only execute below if the Map is not empty (any Status__c changes occured above)

if(!recordsimp.isEmpty(){
accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];
for(account accounts: accountToUpdate){
accounts.status__c = recordsimp.get(accounts.account_num__c).Status__c;
}
if(accountToUpdate.size() > 0){
update accountToUpdate;
}
}

}

This was selected as the best answer
souvik9086souvik9086

It is happening recursive call. Another trigger is calling from this and it is going in infinte loop.

So to avoid this do this things.

 

Create a new controller

 

public class FutureTriggerController{

public static boolean isFutureUpdate = false;

}

 

Trigger

 

trigger updatefield on imp__c (after update) {

if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate =true;


Map<String,imp__c > recordsimp = new Map<String,imp__c >();
List<account> accountToUpdate = new List<account>();

for(imp__c impRecord : trigger.new){
recordsimp.put(impRecord.rec_ID__c,impRecord);
}

accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];
for(account accounts: accountToUpdate){
accounts.status__c = recordsimp.get(accounts.account_num__c).Status__c;
}
if(accountToUpdate.size() > 0){
update accountToUpdate;
}

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

 

Richa_LearningRicha_Learning

Thanks k_bentsen , it worked for me :)

Thanks Souvik :)

But have a question, i f I need to copy 15 fields from IMP__c to Account..how should I do?

 

Should I write all 15 itmes in OR condition?

if(impRecord.Status__c != oldImp.Status__c|| imprecord.abcd__c!=oldimp.abcd__c)

k_bentsenk_bentsen

That should work. It won't look very pretty to have the 15 conditions in an if statement, but it's probably the easiest way to check if any field actually changed on a record, that I know of.

Richa_LearningRicha_Learning

Thanks a lot :)