function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HuluSFAdminHuluSFAdmin 

Triggering Approval processes from APEX code

Hello,

I am trying to programatically trigger Approval Processes when an Opportunity stage is set to "Approval Requested". Here is the snipped of code from my APEX trigger that invoke the Approval Process -

void invokeApprovalProcess(Opportunity o){
         Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
         req.setObjectId(o.id);
         req.setComments('Submitting Opportunity '+o.Name+' for approval');
         System.debug('Submitting Opportunity '+o.Name+' for approval');
         Approval.process(req);
}

But I get the following error when I create an Opportunity and set stage = "Approvals Requested". Any idea what is it that I am doing wrong here.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger insertionOrderTrigger caused an unexpected exception, contact your administrator: insertionOrderTrigger: execution of BeforeUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.: Trigger.insertionOrderTrigger.invokeApprovalProcess: line 141, column 5


HuluSFAdminHuluSFAdmin
Figured it out - I set up the trigger on "Before Update" of Opportunity instead of "After Update"