Sub topic 3
Submitting and Responding to Approval Requests
Assume you know Salesforce objects, users, queues, validation rules, and basic Apex and SOQL. An approval process routes a submitted record to configured approvers. The record can be submitted from the record page, Flow, or Apex. Approvers use the approval request to approve, reject, or sometimes reassign the request.
Approval Data and Apex
Salesforce stores the overall request in ProcessInstance. Each response is stored in ProcessInstanceStep. A pending assignment is represented by ProcessInstanceWorkitem. These records let you inspect status, actors, comments, and dates.
In Apex, Approval.process submits a record or responds to a work item. The submitter must be allowed by the process, and the running user must have access to the target record.
Opportunity opp = [
SELECT Id
FROM Opportunity
WHERE IsClosed = false
LIMIT 1
];
Approval.ProcessSubmitRequest submitRequest =
new Approval.ProcessSubmitRequest();
submitRequest.setObjectId(opp.Id);
submitRequest.setComments('Submitting discount request.');
Approval.ProcessResult submitted = Approval.process(submitRequest);
if (submitted.isSuccess()) {
ProcessInstanceWorkitem item = [
SELECT Id
FROM ProcessInstanceWorkitem
WHERE ProcessInstance.TargetObjectId = :opp.Id
AND ProcessInstance.Status = 'Pending'
LIMIT 1
];
Approval.ProcessWorkitemRequest response =
new Approval.ProcessWorkitemRequest();
response.setWorkitemId(item.Id);
response.setAction('Approve');
response.setComments('Approved by finance.');
Approval.process(response);
}Query ProcessInstance with its child Steps relationship to read approval history. The step records show whether an action was approved or rejected, who acted, comments, and timestamps. A record can have multiple instances when it is recalled and submitted again.
> callout note > A pending work item does not guarantee that the current user can approve it. Check the assigned approver, delegation, permissions, and sharing access.
Testing, Troubleshooting, and Design
Test each path: entry criteria failure, successful submission, every approval step, rejection, recall, and final actions. Check that automation does not update locked fields. Common failures include no matching process, an unauthorized submitter, no pending work item, and a record locked by approval.
Choose approval processes when routing, audit history, and record locking are central. Use Flow for broader automation or dynamic orchestration. Keep criteria and approver rules simple, document who can recall requests, and account for Salesforce governor limits and approval-process limits.
Exercise
Which object should you query to find the current pending approval assignment?
Show answer
Query ProcessInstanceWorkitem and relate it to a pending ProcessInstance for the target record.