You need to sign in to do that
Don't have an account?
Trigger After Account Update
Hello, I am writing a trigger that displays information from the most recently created opportunity on the account page. The fields are dynamic, meaning anytime theres an account update or insert, it looks for the most recently created opportunity, and grabs the necessary info. The trigger works fine, except when you move an opportunity. When moving an opporuntiy, the information displayed on the account page is from the newly moved opportunity (which is incorrect). It does trigger an account update, but it still shows the old/incorrect data. My thought was to write a second after update trigger. Below is the error message i receive when attempting to update any field on the account page. Seems to be an iterative loop problem.
Error:Apex trigger DynamicOppDataOnAcctPageafter caused an unexpected exception, contact your administrator: DynamicOppDataOnAcctPageafter: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001g0000006lRjZAAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DynamicOppDataOnAcctPageafter: maximum trigger depth exceeded Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ] Account trigger event AfterUpdate for [001g0000006lRjZ]: []: Trigger.DynamicOppDataOnAcctPageafter: line 17, column 1
I have included both the before and after triggers below, any help/advice would be greatly appreciated
Before Trigger
trigger DynamicOppDataOnAcctPageBefore on Account (before insert, before update) { Account acct = Trigger.new[0]; String accountid = acct.id; String OpQ = null; String OpA = null; for (Opportunity opp: [SELECT id, security_question__c, security_answer__c FROM opportunity WHERE (AccountID = :accountid) AND (recordtypeid = '01230000000000K' OR recordtypeid = '012600000009I8D' OR recordtypeid = '012600000009FS0' OR recordtypeid = '01230000000000L') ORDER BY user_created_date__c ASC ]){ acct.Security_question_opp__c = opp.security_question__c ; acct.Security_answer_opp__c = opp.security_answer__c ; } }
After Trigger
trigger DynamicOppDataOnAcctPageAfter on Account (after update) { Account acct = Trigger.new[0]; String accountid = acct.id; Map<Id, Account> accountMap = new Map<Id, Account>(); for (Opportunity opp: [SELECT id, accountid, security_question__c, security_answer__c FROM opportunity WHERE (AccountID = :accountid) AND (recordtypeid = '01230000000000K' OR recordtypeid = '012600000009I8D' OR recordtypeid = '012600000009FS0' OR recordtypeid = '01230000000000L') ORDER BY user_created_date__c ASC]){ Account acct1 = new Account(Id=opp.AccountId, Security_Question_opp__c = opp.security_question__c, security_Answer__c = opp.Security_Answer__c); AccountMap.put(acct1.Id, acct1); } if(accountMap.size()>0) { update accountMap.values(); }
Thanks Again!
You are having the common problem of recursive triggers
Solution is here: http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
The after update trigger is updating Accounts that in turn cause the before/after triggers to fire again...and again...
The problem is due to that your trigger is calling recursively. To prevent that you have to create a class where you can keep a static variable and then in the trigger make that variable as true, so that recursive call will be prevented.
Controller
public class FutureTriggerController{
public static boolean isFutureUpdate = false;
}
Trigger
trigger DynamicOppDataOnAcctPageAfter on Account (after update) {
if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate =true;
Account acct = Trigger.new[0];
String accountid = acct.id;
Map<Id, Account> accountMap = new Map<Id, Account>();
for (Opportunity opp: [SELECT id, accountid, security_question__c, security_answer__c FROM opportunity
WHERE (AccountID = :accountid) AND (recordtypeid = '01230000000000K' OR recordtypeid = '012600000009I8D'
OR recordtypeid = '012600000009FS0' OR recordtypeid = '01230000000000L')
ORDER BY user_created_date__c ASC]){
Account acct1 = new Account(Id=opp.AccountId, Security_Question_opp__c = opp.security_question__c, security_Answer__c = opp.Security_Answer__c);
AccountMap.put(acct1.Id, acct1);
}
if(accountMap.size()>0) {
update accountMap.values();
}
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Thanks all, this is running error-free, but not working as expected. The after update trigger doesn't seem to be updating the account pages. I replaced the bolded/underlined code below with a static Account ID, and it worked correcrly. Therefore, I believe theres an issue with the way this map is getting and updating the account pages. Please advice, thanks!
AK123
Your problem statement said you wanted to take the most recently created Oppo and update the Account with data from that Oppo whenver the account is updated
I rewrote your trigger to be bulk aware; I removed the after insert and after delete as they are not relevant here. I'm assuming that you don't have other after update triggers on Account that don't undo this trigger's work as trigger execution order is not deterministic.
I also tried to be clearer in variable names
Also best practice advice is not to use recordtypeIds in code. If you create the recordtypes first in a sandbox, those ids wont be the same when you deploy to PROD. Always use the developerName field instead to locate recordtypeIds before you need to use them - much better documentation for your code
e.g