Approval Process using Apex

In order to use apex code to submit the record for Approval process, we have to first create Approval process in Create –> Workflow & Approvals.


Sample code:

    void submit(Id localId)
    {
        // Create an approval request for the account
   
        Approval.ProcessSubmitRequest req1 =
            new Approval.ProcessSubmitRequest();
        req1.setComments(‘Submitting request for approval.’);
        req1.setObjectId(localId);
       
        // Submit the approval request for the account
   
        Approval.ProcessResult result = Approval.process(req1);
       
        // Verify the result
   
        System.assert(result.isSuccess());
       
        System.assertEquals(
            ‘Pending’, result.getInstanceStatus(),
            ‘Instance Status’+result.getInstanceStatus());       

    }

Leave a Reply