You need to sign in to do that
Don't have an account?
After Update, Insert Trigger
Hi All
I want to update the OwnerId of a record based on a value of picklist (UCB_Status__c).
Normally it would be easy to do with a before insert, before update trigger. But there is already a Before Insert, Before Update trigger in the system for the same object and I can not touch that trigger.
My trigger:
trigger UCB_Coaching_Report_A_I_A_U on Coaching_Report_vod__c (after Insert, after Update) { UCB_ManageCoachingReport.assignOwner(Trigger.newMap.keySet()); }
The trigger that can not be touched:
trigger VOD_COACHING_REPORT_BEFORE_INSUPD_TRIGGER on Coaching_Report_vod__c ( before insert, before update) { if(VEEVA_PROCESS_FLAGS.getUpdateCR()== true){ System.Debug('Veeva process_Flags'); return; } //UCB_ManageCoachingReport.assignOwner(Trigger.New, Trigger.oldMap); for (Integer i = 0 ; i < Trigger.new.size(); i++) { System.Debug('In Veeva for lus'); if (Trigger.new[i].Employee_vod__c != null ) { System.Debug('In Veeva if employee is verschillend van 0'); Trigger.new[i].OwnerId = Trigger.new[i].Employee_vod__c; } } }
The class which contains the invoked method:
public class UCB_ManageCoachingReport{ public static void assignManager(List<Coaching_Report_vod__c> coachingReportList){ for(Coaching_Report_vod__c cr: coachingReportList){ System.Debug('de user met id ' + UserInfo.getUserId() + ' wordt toegewezen aan het mananager veld'); cr.Manager_vod__c = UserInfo.getUserId(); } } public static void assignOwner(Set<Id> crIds) { List<Coaching_Report_vod__c> crList = [select Id, OwnerId, UCB_Status__c, Review_Date__c, Manager_vod__c, Employee_vod__c from Coaching_Report_vod__c where Id in:crIds]; for(Coaching_Report_vod__c cr: crList ) { if(cr.UCB_Status__c == 'Sent') { cr.OwnerId = cr.Employee_vod__c; } else { cr.OwnerId = cr.Manager_vod__c; } } update crList; } }
When I create a new Coaching Report I get the following error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UCB_Coaching_Report_A_I_A_U caused an unexpected exception, contact your administrator: UCB_Coaching_Report_A_I_A_U: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a1DZ0000000WFLmMAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UCB_Coaching_Report_A_I_A_U: maximum trigger depth exceeded Coaching_Report_vod trigger event AfterInsert for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm] Coaching_Report_vod trigger event AfterUpdate for [a1DZ0000000WFLm]: []: Class.UCB_ManageCoachingReport.assignOwner: line 33, column 1
I spend multiple our, changed my code 23235235253 times and still don't got a solution.
Is it just not possible due to the trigger I can not change?
Thanks in advance!!!
You can write multiple triggers on same event. So what if you cannot touch an already existing before insert/before update code, go ahead and create one more trigger with the same events.
Hi,
Try usign the Trigger.IsBefore / Trigger.IsAfter and so on.
Click here to take a look on Trigger Context Variables usage
Hope so this helps you...!
Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.
Remove the update statement. If you are updating fields on a before trigger object, then no need to do an update.