Dynamic Approval Process in Salesforce based on the Apex and Trigger

Although this is very common approach and lots of articles are around on this topic, still I want to delineate the topic in other way. This topic covers complete scenarios for the approval process based on the Apex class.

Agenda of this article:

  1. Automatically submit the record for approval on the basis of field value.
  2. Automatically select the next Approver.
  3. Approve / Reject the record on the basis of field.

Assumptions:

  • Opportunity Object is used.
  • Approval Process is already set on the Opportunity.
  • Field “Next_Approver” will decide that who is going to approve the record.
  • There are three steps in the approval process.
  • There is no test class written and no check for mandatory fields needed for the trigger, as I have considered positive scenarios only.

Important URLS:

API of Approval Process classes:

  1. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process.htm
  2. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ProcessRequest.htm
  3. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ProcessResult.htm
  4. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ProcessSubmitRequest.htm
  5. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ProcessWorkitemRequest.htm

Steps of Standard approval process defined:

Approval Process Steps

Approval Process Steps

To achieve this, I am going to create the trigger named “AutomateApprove”.

Automatically submit the approval process using trigger – Apex:

Below method is used to automatically submit the approval process using trigger.

public void submitForApproval(Opportunity opp)
    {
        // Create an approval request for the Opportunity
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(new Id[] {opp.Next_Approver__c});

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);

    }

Class “ProcessSubmitRequest“ is used to automatically submit the approval process. We need to set following items while submitting the approval process using trigger:

  • Comment
  • TargetObjectId
  • NextApproverIds – if needed. Here Custom logic can be written to dynamically set approver for approval process. In this case I am using the custom field present on the Opportunity.

Automatically approve the approval process using trigger – Apex:

Below method is used to automatically approve the approval process using trigger.

/*
    * This method will Approve the opportunity
    */
    public void approveRecord(Opportunity opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');
        req.setNextApproverIds(new Id[] {opp.Next_Approver__c});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

Class “ProcessWorkitemRequest“ is used to automatically approve the approval process. We need to set following items while submitting the approval process using trigger:

  • Comment
  • TargetObjectId
  • NextApproverIds – if needed
  • WorkItemId – Custom code required to get this

Get the WorkItemId for the pending approval process of the Object:
This is the tricky part, if the Submission and approval of the record is done in single code block then it’s very easy to get the WorkItemId of the needed process.

Here the standard code snap provided:

After Submission the approval process using Apex we get the object of class “ProcessResult“.

Approval.ProcessResult result = Approval.process(req1);

And from the class we can get workitemid as :

List<Id> newWorkItemIds = result.getNewWorkitemIds();

And set the id like:

req2.setWorkitemId(newWorkItemIds.get(0));

Other method to get the “WorkItemId” :
The above code was not usable in our scenario as the submission and approval or rejection was done at different level. So I have created following utility method to get the WorkitemId of the supplied Object’s id. Here I have considered that only one workitem will present.

public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }

As you can see, we need to query the object “ProcessInstanceWorkitem“ to get workitemId of the object.

Automatically reject the approval process using trigger – Apex:
Following code is used to reject the approval process using code.

public void rejectRecord(Opportunity opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        //req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

Execution of Approval process using Apex and trigger:

Approval Process Log After Execution

Approval Process Log After Execution

Complete code:

trigger AutomateApprove on Opportunity(After insert, After update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        //Insure that previous value not equal to current, else if there is any field update on approval process action
        //there will be recurrence of the trigger.

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Approve' && Trigger.old[i].Next_Step__c != 'Approve')
        {
             approveRecord(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Reject' && Trigger.old[i].Next_Step__c != 'Reject')
        {
             rejectRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

    // This method will submit the opportunity automatically
    public void submitForApproval(Opportunity opp)
    {
        // Create an approval request for the Opportunity
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(new Id[] {opp.Next_Approver__c});

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);

    }

    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }

    // This method will Approve the opportunity
    public void approveRecord(Opportunity opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');
        req.setNextApproverIds(new Id[] {opp.Next_Approver__c});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

    // This method will Reject the opportunity
    public void rejectRecord(Opportunity opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        //req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

}

Note on possible errors:

1.If you have the “manual Selection of approver” enabled for your approval process/steps then you must specify the approver in the trigger, else you will get an error something like:

“System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []”

2.If you set the wrong WorkitemId then may get following error:

Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Related posts

  • Royampatuan

    Can we have hide-when formula on custom buttons?

    • JitendraZaa

      We cannot hide / unhide custom buttons on Standard Page layout

  • Ramarajur26

    Thank you Very much . I used this code  working fine and Errors which you have given helped me allot. thank you

  • Solees

    This really helped me a lot, thank you so much my friend !!!

  • aman

    My requirement is to add multiple approvers when i am going to set multiple approvers for a particular code. It showing me a error of Required Field missing.
    List userIds=new List();

    userIds.add(‘xxxxxxxxxxxxxxx’));
    userIds.add(‘xxxxxxxxxxxxxxx));
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments(‘Submitting request for approval automatically using Trigger’);
    req1.setObjectId(objId);
    req1.setNextApproverIds(userIds);
    // Submit the approval request for the Opportunity
    Approval.ProcessResult result = Approval.process(req1);

    Getting this Error…
    “System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []”

  • shaambo

    Dude..this is shaambo….how are you??..:)
    .was wondering is there no way apart from writing a trigger to automatically submit a approval process apart from writing a trigger?..

    • JitendraZaa

      Hi Shaamboo,

      Yar, I dont think that there is any other way. We have to call this method “Approval.ProcessSubmitRequest();”

  • Jaspreet Singh

    but this will work only for three step approval. What if there are more approvers. Is there a way to create approval steps dynamically?

  • madhu

    hi Jit.this is madhu.i have small dought.
    how to we can attach pdf’s in lead object.can u suggest me pls.

    • JitendraZaa

      Hi Madhu,
      You can attach PDF to Lead in “Notes and Attachment” section if its less than 5MB.

  • Beginer

    public Id getWorkItemId(Id targetObjectId)
    {
    Id retVal =null;
    for(ProcessInstanceWorkitem workItem : [Select p.Id from ProcessInstanceWorkitem
    where p.ProcessInstance.TargetObjectId =: targetObjectId])
    {
    retVal = workItem.Id;
    }
    return retVal;
    }

    I am getting null value(for loop) and i am using this—req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
    please help me

  • Dinesh Singla

    hi ,
    can we get the approval Status in trigger.
    means approval process is in pending,rejected or approved state

    • JitendraZaa

      Simplest solution – create hidden field of type text. On approval process update this field properly on stages. Use this field inside trigger for any post processing

      • Dinesh Singla

        Thanks

  • Dinesh Singla

    hi ,
    in Campaign there is a look up of parent Campaign. in this look up its showing all the active Campaign,All Campaign can we hide that drop down and can we use our filter criteria ?

  • Lee

    I’m new to the approval process, so this was probably obvious to others but it was not to me and/or I’m getting something wrong.

    In order for the step to work where you explicitly set the approver, you have to set the approval process approval step to “manual Selection of approver”, anything else and it won’t work.

    thanks for the great code!

  • Dipak Nikam

    How do we write a test method for the above code. I tried added above code and its working fine in my case. But I am not getting expected test coverage for above piece of code. I know the issue, in test method ProcessInstanceWorkitem id coming as null so I am facing validation error given in else loop. I tried adding seeallData = true so that test method will look into organization configuration, but I am still facing issues.
    Do I have to use test.isRunningTest attribute to ignore piece of code in test method.

    Thanks in advance and nice post !!!!

  • Dinesh

    Hi, I have a query about this… an object like opportunity may have multiple approval processes set up. How does apex pick the correct approval process when the process name is not mentioned anywhere in the code?

  • Atikrant

    How to get Approvals or Approval Process using REST API?