Dynamic Approval Process in Salesforce

Dynamic Approval Process

Dynamic Approval Process in Salesforce: Allocate the approvers dynamically to the record. Populate the approvers in the user lookup fields in the record.
In static approval process, we have to define the approvers in the approval process. Meaning whenever the record matches the approval process condition and approval process fires and associate the record to the approver which we define in approval process.

Always we cannot achieve business criteria with static approval process, meaning we have conditions like record should be routed to different approvers based on region, country or with some other criteria. We have to write one static approval process for each condition.

So to avoid multiple static approval process for the same requirement with same kind of implementation we will go for dynamic approval process.
– This process is full of apex and implementation is somewhat tricky but very powerful and useful for the huge projects.
– We have to write apex (triggers) to populate the approver in the record lookups.
– To achieve dynamic approval process we have to create one new object (approval matrix), which is useful to define all the conditions (region, country, etc…) and the approvers.

So once user clicks on SUBMIT button. We should fires apex code and check what and all the approval matrix matches to the record and we will take the approvers from the approval matrix and populate the approvers into the user lookups in the record, then we will fire the approval process. Inside approval process we have to define the steps like route process instance to approvers.

Example: Create an approval process on opportunity object, should be routed to base on Country. (Let say we have almost 200 countries in our project)

Steps to Implementation of dynamic approval process
1. Create User lookup fields and one picklist field (Status__c and values are Open, Submitted, Approved and Rejected).
2. Create an Approval process on Opportunity Object with Entry criteria is Status equal to Open.

3. Create one new Object called Approval Matrix to define all the conditions
4. Execute the below code on submit button.

public class OpportunityApprovalMatrix{
public static list<Approval.ProcessResult> ApprovalMatrixMatch(Set<Id> OpportunitySet){
List<Opportunity> updatedOpptyList = new List<Opportunity>();
Set<String> countrySet = new Set<String>();
List<Id> OpptyIds = new List<Id>();
Map<Id,Approval_Matrix__c> approvalMatrixMap=new Map<Id,Approval_Matrix__c>();
List<Opportunity> opptyList = [SELECT Id,Name,country__c,status__c,Approver1__c,Approver2__c FROM Opportunity where Id IN:OpportunitySet];
for(Opportunity opptyRec :opptyList){
opptyRec.Approver1__c = ”;
opptyRec.Approver2__c = ”;
countrySet.add(opptyRec.Country__c);
}
List<Approval_Matrix__c> approvalMatrixList = [SELECT country__c,Approver1__c,Approver2__c from Approval_Matrix__c where Country__c IN :countrySet];
for(Opportunity opptyRec :opptyList){
for(Approval_Matrix__c approvalMatrix :approvalMatrixList ){
if(opptyRec.country__c == approvalMatrix.country__c){
opptyRec.Approver1__c = approvalMatrix.Approver1__c;
opptyRec.Approver2__c = approvalMatrix.Approver2__c;
opptyRec.status__c = ‘Open’;
updatedOpptyList.add(opptyRec);
OpptyIds.add(opptyRec.Id);
}
}
}
update updatedOpptyList;
list<Approval.ProcessSubmitRequest> submitOpptyList = new list<Approval.ProcessSubmitRequest>();
for(Id oppId: OpptyIds){
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments(‘Submitting request for approval.’);
req.setObjectId(oppId);
submitRequestList.add(req);
}
Approval.process(submitRequestList);
}
}

Notes:
– This code only checks country level, if business requirement has many conations with different objects related to opportunity then we create one more object child to approval matrix and add all the conditions there.
– We can implement many approvers you need and levels as well in this process
– We can assign to the group of user (Queue instead on single user) also in this process.
– In this process we can get many matching record with our criteria, so we can add couple of fields in approval matrix like “threshold” and need to modify the logic based on this. Meaning if there are three matrix are matched for the opportunity then we can filter based on Threshold values which is maximum.
– Some cases we won’t find any matrix matches, in this case we can create default matrix. If any match is not found then we can route to this matrix.

One thought on “Dynamic Approval Process in Salesforce”

Leave a Reply