Sub topic 13
Reading approval history
This chapter assumes you know how an approval process runs and how the approval objects relate: ProcessInstance, ProcessInstanceStep, and ProcessInstanceWorkitem. You also need basic SOQL. Here you read the history of past approvals, both on the record page and in code and reports.
What approval history records
Each time a record is submitted, Salesforce creates a ProcessInstance. As people act, it adds a ProcessInstanceStep for each completed action. Pending requests live in ProcessInstanceWorkitem until someone responds.
| Object | What it tells you |
|---|---|
| ProcessInstance | One submission: its status, target record, and completion date |
| ProcessInstanceStep | One action taken: the actor, outcome, comments, and time |
| ProcessInstanceWorkitem | One request still waiting for a response |
A record resubmitted after a rejection gets a new ProcessInstance. Older instances stay in place, so history accumulates.
The Approval History related list
Add the Approval History related list to the page layout so users see the steps without leaving the record. It shows each step's date, status, assigned approver, actual approver, and comments. The list also has the buttons for approving, rejecting, or reassigning a pending request.
The assigned approver and the actual approver can differ. This happens when a delegated approver responds or when an admin acts on someone's behalf.
Querying history with SOQL
To read history in code, query ProcessInstance and use a child relationship subquery for its steps.
Id opportunityId = '006000000000001AAA'; // replace with a real Opportunity Id
List<ProcessInstance> instances = [
SELECT Id, Status, CompletedDate,
(SELECT StepStatus, Actor.Name, Comments, CreatedDate
FROM Steps
ORDER BY CreatedDate)
FROM ProcessInstance
WHERE TargetObjectId = :opportunityId
ORDER BY CreatedDate DESC
];
for (ProcessInstance pi : instances) {
for (ProcessInstanceStep s : pi.Steps) {
System.debug(s.Actor.Name + ': ' + s.StepStatus + ' - ' + s.Comments);
}
}The query filters on TargetObjectId, the Id of the record that was submitted. The newest instance comes first because of ORDER BY CreatedDate DESC.
The first step of each instance usually has the status Started. That step records the submission itself. Later steps show values such as Approved, Rejected, Reassigned, or Removed (for a recall).
Instance status versus step status
These two fields are easy to confuse.
ProcessInstance.Statusdescribes the whole submission. Typical values arePending,Approved,Rejected, andRemoved.ProcessInstanceStep.StepStatusdescribes one action within it.
A multi-step process can have several approved steps while the instance is still Pending. The instance changes to Approved only after the final step is approved.
Reporting on approvals
Standard report types cover approvals in the UI. A report on Opportunities with the approval history lets you filter by step status or approver. Use SOQL when you need data inside automation, for example to find who rejected a request and copy the comment to the record.
Common errors
- Empty results. You queried by the wrong Id.
TargetObjectIdmust be the submitted record's Id, not the ProcessInstance Id. - Missing comments. Comments are stored on the step only if the user entered them. Check for null before using them.
- Counting rows as submissions. One submission has several steps. Count ProcessInstance rows to count submissions.
- Visibility gaps. Users without access to the target record may not see its history rows.
Exercise
A record was rejected at step 2, then the owner fixed it and resubmitted. How many ProcessInstance records exist for it, and how do you get the latest one first?
Show answer
Two ProcessInstance records exist, one per submission. Query with WHERE TargetObjectId = :recordId and ORDER BY CreatedDate DESC, so the resubmission comes first.