You need to sign in to do that
Don't have an account?
Issue with Trigger for submission to Approval Flow
Hi,
I have an issue with the below trigger.
When the Stage on the Opportunity is changed to 'Won' or 'Lost' the opportunity enters an approval process.
However, when the approver tries to change any information on the opportunity (after unlocking it) he gets the following error:
Error:Apex trigger OpportunitySubmitForOrderListApproval caused an unexpected exception, contact your administrator: OpportunitySubmitForOrderListApproval: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []: Trigger.OpportunitySubmitForOrderListApproval: line 10, column 1
To me it seems it is trying to submit the opportunity again and as it is still under approval brings the error message.
Can anyone see in the code below any errors regarding this?
Thanks!!
trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) { for(Integer i = 0; i < Trigger.new.size(); i++) { if ((Trigger.old[i].StageName <> '6 - Project won' || Trigger.old[i].StageName <> '7 - Project lost') && (Trigger.new[i].StageName == '6 - Project won' || Trigger.new[i].StageName == '7 - Project lost')) { Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest(); req.setComments('Submitted for approval. Please approve.'); req.setObjectId(Trigger.new[i].Id); Approval.ProcessResult result = Approval.process(req); System.assert(result.isSuccess()); } } }
Hi,
I think I figured it out and adjusted your proposal slightly.
Many thanks for your help!!!!
All Answers
Looks like, you are trying to create another approval after update, thats causing the problem.
Trigger approval only when it is set for first time, using Trigger.new[i].StageName <> Trigger.old[i].StageName
Here is the new code...
Many thanks for your feedback...
I am not sure I understand the if statement in your code as it only refers to Trigger.old[i] and not Trigger.new[i] at all?
Hi,
I think I figured it out and adjusted your proposal slightly.
Many thanks for your help!!!!
sorry for hurry burry reply...