You need to sign in to do that
Don't have an account?
bikla78
Approval Process- APEX
I have created a custom object called PTO. I have also created an approval process for it. How can I create a trigger so that it automically executed the approval process i have setup as soon as a new PTO record is created(inserted). This would behave the same way as if I manually pressed the submit approval button.
trigger PTO_Trigger on PTO__c (after insert)
{
for(PTO__c pto: [select id from pto__c])
{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(pto.id);
}
}
Ok, here we go. I got it to work: Here it is, if anybody else wants to see the solution to bypass the submit approval button
All Answers
You need to add the below line.
Approval.ProcessResult result = Approval.process(req1);
I added the line but I am getting error below:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger PTO_Trigger caused an unexpected exception, contact your administrator: PTO_Trigger: execution of AfterInsert caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []: Trigger.PTO_Trigger: line 7, column 36
Ok, here we go. I got it to work: Here it is, if anybody else wants to see the solution to bypass the submit approval button
Thank you for posting this code. I am using the lock part of the approval process to lock down closed opportunities so that no one can change them except the admin. Is there also a way to trigger automatic approval following the automatic submittal?
Thanks!
This is perfect! Thanks for sharing :smileyvery-happy:
Great, time saving
Hi ,
But same pice of code when i using After Update event not working and same error im getting.
i dont think this is the right answer how the problem is solved if we add system.assert equals...its just a assert statement
It isn't a solution. If you use after update, you still receive this error. If you use after insert, it doesn't go thru the approval process (at least mine didn't).