Salesforce Approval Process

Approval Process

An approval process automates how records are approved in Salesforce. An approval process specifies each step of approval, including from whom to request approval and what to do at each point of the process.

Salesforce Approval Processes allow organizations to automate their approval processes for records, such as leads, opportunities, and custom objects. Here's an example of how you can create an Approval Process for Opportunity records and programmatically submit them for approval using Apex:

Define Approval Process in Salesforce Setup:

Navigate to Setup > Create > Workflow & Approvals > Approval Processes.

Create a new Approval Process for the Opportunity object.

Define entry criteria, approval steps, approval actions, and any additional criteria or actions as needed.

Activate the Approval Process.

Submit Opportunity for Approval using Apex:

public with sharing class OpportunityApprovalService {
    
    // Method to submit an Opportunity for approval
    public static void submitOpportunityForApproval(Id opportunityId) {
        // Query the Opportunity record
        Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Id = :opportunityId LIMIT 1];
        
        // Create an instance of ProcessSubmitRequest to submit the record for approval
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setComments('Submitting Opportunity ' + opp.Name + ' for approval.');
        req.setObjectId(opportunityId);
        
        // Submit the record for approval
        Approval.ProcessResult result = Approval.process(req);
        
        // Check if the submission was successful
        if (result.isSuccess()) {
            System.debug('Opportunity ' + opp.Name + ' submitted for approval successfully.');
        } else {
            System.debug('Error submitting Opportunity ' + opp.Name + ' for approval: ' + result.getErrors()[0].getMessage());
        }
    }

}

Call the Apex Method from Your Code:

// Example code to call the OpportunityApprovalService.submitOpportunityForApproval() method
Id opportunityId = '006XXXXXXXXXXXX'; // Replace with the Opportunity record Id
OpportunityApprovalService.submitOpportunityForApproval(opportunityId);

Apex allows to programmatically submit Opportunity records for approval using Apex code. It can be customize method further to handle specific requirements or integrate it into your business processes as needed.


Links -


Approver Actions Based on the Approval Setup

Approve or reject based on the first response.
The first response to the approval request determines whether the record is approved or rejected.

Require unanimous approval from all selected approvers.
The record is approved only if everyone approves the request. If any approvers reject the request, the approval request is rejected.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach