This article explain the Automatic submission of Approval process using Apex and trigger. It include Automatic submission, approval as well as rejection of record completely using 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:

Automatically submit the record for approval on the basis of field value.

Automatically select the next Approver.

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.

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.

Question :1. Can we add multiple users (Parallel Approval process) as a aprrover automated using above code?
Ans : No. Logic in above code is that we need to select next approver option as “manual”. Currently we cannot use multiple users manually in approval process, you can vote this idea for this feature support. Only solution is to have multiple steps for each approver.

Hi this may be a comment too late and also may be known to you by now. Just a solution that would help other folks.
We can create a button and set execute Javascript and call apex class through soap sforce.execute()

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.

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?

Hi Jithendra,
I am having two custom objects A and B.These two are in Lookup Relationship. I am creating a approval process on Object B ,I need the approver to be record owner of Object A. To achieve this i created a field on object B with lookup to User Object.with trigger i am updating the owner on Object B and submitting for approval .if the owner of the object A record is user then my scenario is working fine if it owner is ‘Queue’ then how can i achieve this ..
please help me out…

Whilre replicating the above blog in my env as practice, I observed that TargetObjectId was not required while providing approval via Apex. Just want to bring it to your attention as you have mentioned that its needed. It might have become optional after few release. Very helpful though 🙂 Thanks !

Will Submitting an approval process automatially through Apex Trigger , causes it the record to be unlocked? As when i am implementing it , unlike the standard behaviour- record trigger for Approval remenained unlocked… What will you advice?