You need to sign in to do that
Don't have an account?
Fire trigger on record creation
Hi,
We have a trigger which submits an oportunity automatically for approval if the 'Stage' is changed to either 'Won' or 'Lost' from some other stage status.
This part of the trigger works fine.
What we would like to implement as well is that when an opportunity is entered with the Stage set to 'Lost' (or 'Won') directly the submission to approval should also be triggered. So on creation the trigger should check if Stage equals 'Won' or 'Lost'
I am not sure how to write the IF statement for this instance.
It would need to include something like the ISNEW( ) function which however is only available in formulas.
Ideally if possible both functionalities, the existing and the check on record entry would be dealt with in one trigger.
Any help is appreciated!
Many thanks...
Trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) { for(Integer i = 0; i < Trigger.new.size(); i++) { if((Trigger.new[i].StageName == 'Won' && Trigger.old[i].StageName <> 'Won') || (Trigger.new[i].StageName == 'Lost' && Trigger.old[i].StageName <> '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()); } } }
try this
All Answers
try this
Thanks Ross,
Seems to be working perfectly!
Best regards,
Patrick