Mastering Approval Processes in Salesforce with Apex

by Rijwan Mohmmed
0 comment
mastering-approval-processes-in-salesforce-with-apex-techdicer

Helloo floks, Today we will explore Mastering Approval Processes in Salesforce with Apex. An Approval Process in Salesforce is an automated workflow that enables organizations to systematically approve records. While the standard approval process feature handles most approval requests and decisions, there are same situations where we can use Apex for create process and update Approval process status. An approval process automates how records are approved in Salesforce.

An approval process specifies each step of approval, including who to request approval from and what to do at each point of the process.

Also check this: Integrating External Data Seamlessly with Salesforce External Objects

What is an Approval Process in Salesforce?

An approval process in Salesforce automates the routing of records for review, ensuring that specific conditions are met before a record advances to the next stage. These processes are frequently used for tasks such as expense approvals, contract management, and custom workflow approvals.

Why We Use Apex for Approval Processes?

Salesforce offer point-and-click tools a powerful way to create approval processes, there are some scenarios where you might need to use Apex for more complex logic or custom requirements:

  1. Complex Business Logic: If approval criteria involve intricate conditions or multiple related records.
  2. Custom Notifications: When you need to send custom email alerts or messages based on specific criteria.
  3. Automated Approvals: When approvals need to be automated based on data or external triggers.

Approval Process Object Model

  1. ProcessInstance
  2. ProcessInstanceStep
  3. ProcessInstanceWorkitem
  4. Approval
mastering-approval-processes-in-salesforce-with-apex-Approval-process-object-ER-techdicer

Submitting Records for Approval in Apex Class

public class ApprovalProcessHandler {
	
    public static String submitApprovalProcessRequest(String objectRecordId) {
        String message;
        // Initialize the approval request
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setComments('Submitted for approval via Apex');
        req.setObjectId(objectRecordId); // Replace with the ID of the record you want to submit
        //req.setNextApproverIds(new List<Id>{}); approval process Approver

        // Submit the record for approval
        Approval.ProcessResult result = Approval.process(req);
        
        // Check the result
        if (result.isSuccess()) {
            message = 'Record submitted for approval successfully.';
        } else {
            message = 'Failed to submit the record for approval.';
        }
        System.debug('message=>'+ message);
        
        return message;
    }
}

Approve and Reject Approval Process Request in Apex

public class ApprovalProcessHandler {
    
    public static String approveRejectApprovalProcessRequest(String objectRecordId, String status) {
        String message;
        // Query the pending approval request
        ProcessInstanceWorkitem workItem = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :objectRecordId LIMIT 1];
        
        // Initialize the approval request
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approved/Rejected via Apex');
        req.setAction(status); // Use 'Reject' to reject and Approve for approve record.
        req.setWorkItemId(workItem.Id);
        
        // Approve the record
        Approval.ProcessResult result = Approval.process(req);
        
        if (result.isSuccess()) {
            message = 'Record approved/rejected successfully.';
        } else {
            message = 'Failed to approve/reject the record.';
        }
		System.debug(message);
        
        return message;
    }
}

Best Practices for Approval Processes in Apex

  1. Error Handling: Always include robust error handling in your Apex code to manage any issues during the approval process.
  2. Bulk Processing: Ensure your Apex code can handle bulk records to avoid hitting governor limits.
  3. Testing: Write comprehensive unit tests to cover all scenarios, including edge cases where approvals might fail or require special handling.

Reference:

  1. Approval Processing

What’s your Reaction?
+1
1
+1
0
+1
0
+1
0
+1
1
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.