top of page

How To Build An Approval Process In Salesforce?

In this blog we will talk about the approval process, an approval process is an automated process that automates how records are approved in your organization. It includes a set of steps for a record to be approved/rejected by the user, groups, etc.


In programming to interact with Approval Processes, Salesforce provides several standard objects for the customization of the approval process to help developers.




These are the most commonly used standard objects for the Custom Approval Process in Apex:-


  1. ProcessDefinition

  2. ProcessInstance

  3. ProcessInstanceHistory

  4. ProcessInstanceNode

  5. ProcessInstanceStep

  6. ProcessInstanceWorkitem


Normally, in Salesforce these objects are not visible from the object manager. We can access these objects and their fields from the developer console or by querying the object.


1. ProcessDefinition

In Salesforce, ProcessDefinition is used to represent the definition/process of an approval process.


To query ProcessDefintion, you can use this sample query:-

SELECT Id, Name, Type, Description FROM ProcessDefinition


2. ProcessInstance

In Salesforce, ProcessInstance stands for a particular occurrence of an approval process. It comes into existence when a record is sent for approval and holds details about where that approval process currently stands for that specific record. Essentially, the ProcessInstance acts as a picture of how far along the approval process is for a particular record at any given time.

To query ProcessInstance, you can use this sample query:-

SELECT Id, ProcessDefinitionId, TargetObjectId, Status, CompletedDate FROM ProcessInstance


3. ProcessInstanceHistory

In Salesforce, ProcessInstanceHistory represents the series of events that transpired within a specific approval process instance. It serves as a detailed log of all actions made during the approval process, including approvals, rejections, reassignments, and recalls.


It is crucial to remember, however, that ProcessInstanceHistory does not support direct querying.

In simple terms, This read-only object shows all steps and pending approval requests associated with an approval process (ProcessInstance).


If you query ProcessInstanceHistory directly you will receive an error message-



4. ProcessInstanceNode

In Salesforce, the ProcessInstanceNode represents a specific stage or step in an approval process. Its objective is to provide insight into the approval process structure and the order in which approval processes occur. This object helps to understand how the approval process is arranged and the procedures required for approvals to progress.


To query ProcessInstanceNode, you can use simple query-

SELECT Id, ProcessNodeId, ProcessInstanceId, NodeStatus, ProcessNodeName FROM ProcessInstanceNode


5. ProcessInstanceStep

In Salesforce, the ProcessInstanceStep represents a step instance in an approval process (ProcessInstance) on which users have already acted. It provides information on the current state of each individual approval step within a given approval process instance target object. This data assists in tracking and understanding the current status of each step in the approval process.


To query ProcessInstanceStep, you can use the sample query-

SELECT Id, StepStatus, ProcessInstance.TargetObjectId FROM ProcessInstanceStep WHERE ProcessInstance.TargetObjectId = ‘00022SADSD3233232’

Note - Use your relatable TargetObjectId


6. ProcessInstanceWorkitem

In Salesforce, the ProcessInstanceWorkitem represents a pending job or action assigned to a specific user within a ProcessInstance. It contains information on the approval requests that have been delegated to users as part of the approval process. This object primarily contains information on the tasks that are pending action or approval by certain people who are participating in the approval process.


To query ProcessInstanceWorkitem, you can use this sample query-

SELECT Id, ProcessInstanceId, OriginalActorId, ActorId FROM ProcessInstanceWorkitem


Challenges in the Custom Approval Process


Custom Approval Process In Apex

In the apex test class, we cannot create records for these objects. We can use the see-all data true condition, but we know that it is not a best practice for creating a test class. Below is an apex class and its test class to cover the apex class scenarios in a best practice.


Apex Class

Here is an example of an apex class-






Apex Test Class

Below is the test class of the above class-





Solution

In the Apex test class, most of the developers use see all data true condition but it is not recommended as it does not come under best practices. So the solution to the above problem is that we can fetch these objects in the test class same as metadata.


Summary

This blog covers mostly about approval process and objects related to it. We’ve discussed the objects, how to query these objects, and how to write a test class based on the apex class that uses these objects.


For the latest blog updates follow our blogs

Комментарии


bottom of page