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:
- Complex Business Logic: If approval criteria involve intricate conditions or multiple related records.
- Custom Notifications: When you need to send custom email alerts or messages based on specific criteria.
- Automated Approvals: When approvals need to be automated based on data or external triggers.
Approval Process Object Model
- ProcessInstance
- ProcessInstanceStep
- ProcessInstanceWorkitem
- Approval
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
- Error Handling: Always include robust error handling in your Apex code to manage any issues during the approval process.
- Bulk Processing: Ensure your Apex code can handle bulk records to avoid hitting governor limits.
- Testing: Write comprehensive unit tests to cover all scenarios, including edge cases where approvals might fail or require special handling.