You need to sign in to do that
Don't have an account?
apsullivan
Conflicting Trigger Exception "Cannot Recursively Update Itself"
I'm new to development and I've written a trigger to update the Account with a field when a) the field is blank and b) a Contact is created or updated and contains a value in a corresponding field. The idea being to maintain the data in two places. However, I'm getting an exception which references "kugadd" which I assume is from the Kugamon package we have installed. Can anyone help?
Here is my trigger:
trigger OnAccountContactCategory on Contact (before insert, before update) { List<Id> AccountsToUpdate = new List<Id>{}; // Find account to update for(Contact c: Trigger.new){ if (c.Categorycontact__c != Null && c.AccountId != Null) { AccountsToUpdate.add(c.AccountId); } // Pull field data from account List<Account> accts = new List<Account>([SELECT Id, Category__c FROM Account WHERE Id IN :AccountsToUpdate]); // Update account for(Account a: accts) { if (a.Category__c == Null) { a.Category__c = c.Categorycontact__c; update a; } } } }
Here is the emailed exception (I removed the IDs themselves):
OnAccountContactCategory: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id [ACCOUNT ID]; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, kugadd.AccountAfterInsertAfterUpdate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id [CONTACT ID]; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = [CONTACT ID]) is currently in trigger OnAccountContactCategory, therefore it cannot recursively update itself: [] (kugadd) : [] Trigger.OnAccountContactCategory: line 15, column 1
Thoughts? Thanks!
Try this:
trigger OnAccountContactCategory on Contact (before insert, before update) {
Set<Id> AccountsToUpdate = new Set<Id>{};
// Find account to update
for(Contact c: Trigger.new){
if (c.Categorycontact__c != Null && c.AccountId != Null) {
AccountsToUpdate.add(c.AccountId);
}
}
// Pull field data from account
List<Account> accts = new List<Account>([SELECT Id, Category__c FROM Account WHERE Id IN :AccountsToUpdate]);
List<Account> accountUpdateList = new List<Account>();
// Update account
for(Account a: accts) {
if (a.Category__c == Null) {
a.Category__c = c.Categorycontact__c;
accountUpdateList.add(a);
}
}
if(accountUpdateList.size() > 0)
update accountUpdateList;
}
Please mark this as solution if you are able to solve your problem, so it might would help others too.
Thanks,
Neetu Bansal
Hi Neetu,
First of all thank you for taking the time out to help! I was getting an error "Variable c.Categorycontact__c does not exist" which appeared on line 17. I was able to resolve this by moving braces around, and the trigger appears to be working as expected when I manually cause it, which my prior code did, as well.
I'm realizing now, also, that it looks like what throws this exception is an action taken by Kugamon I believe. I may have to dig a bit deeper. It looks like the Kugamon action is what causes the exception, though.
Here is your code with changes mentioned, also:
I don't believe there are any custom triggers on the account object, but there may be a manager trigger on it. Could it be that the Kugamon Account/Contact Sync package is attempting to update the account at the same time that my trigger is? Would that cause a conflict?
Alex
please refer, it may help you
http://salesforcedevelopersheaven.blogspot.in/2011/12/prevent-recursion-of-trigger-execution.html
http://salesforcedevelopersheaven.blogspot.in/2012/11/prevent-recursion-of-trigger-execution.html
public static Boolean EXECUTE_OnAccountContactCategory_TRIGGER = true;
Default value for thsi will be always true. In end of your trigger, you can set it as false before updating the account. So it will stop the recusive execution.
trigger {
if(Helper.EXECUTE_OnAccountContactCategory_TRIGGER == true) {
//youe logic
Helper.EXECUTE_OnAccountContactCategory_TRIGGER = false;
update a;
}
}
yes, Same thing is decsribed in above blog links
Ex:
Thanks to everyone for the help. Following the instructions in the Mindfire blog post from Dipak, I've added the additional classes to circumvent the recursive update. First of all, does this look correct? If not, should I be removing from my trigger the "update" portion (bolded below)? I'm wondering if I can actually just revert to my original class from the first post except with the FutureUpdateController condition since the extra layers added initially did not resolve the issue.
Here's where I am at now:
Trigger:
Future Update Controller class:
Future Methods Processor class:
Thanks again for the help!!
Won't that just land me back in the same spot where conflicts are occurring? By the way, on my sandbox environment, that code is working as expected, which satisfies sort of the core requirement, but whether it circumvents the exception when the Kugamon action executes is yet to be determined.