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() methodId opportunityId = '006XXXXXXXXXXXX'; // Replace with the Opportunity record Id
OpportunityApprovalService.submitOpportunityForApproval(opportunityId);
Comments
Post a Comment