Sub topic 11
Where Approval Data Is Stored
This chapter assumes you know objects, fields, and records, and can read basic SOQL. You should also have seen how a record is submitted and approved in the UI. Here you will learn which objects hold approval data, how they relate, and how to query them.
Why it matters
When a user submits a record, Salesforce does not add fields to that record. It creates separate records in a small set of system objects. Reports, Apex, and troubleshooting all depend on knowing these objects.
The three core objects
| Object | What one record represents | Useful fields |
|---|---|---|
| ProcessInstance | One run of an approval process on one record | Status, TargetObjectId, CompletedDate, ProcessDefinitionId |
| ProcessInstanceStep | One completed action in that run (submit, approve, reject, recall, reassign) | StepStatus, Actor, OriginalActor, Comments, CreatedDate |
| ProcessInstanceWorkitem | One pending approval request assigned to one approver | ProcessInstanceId, ActorId, OriginalActorId |
Two more objects appear less often. ProcessInstanceNode describes a step in the process definition as it ran. ProcessDefinition describes the process itself.
How they relate
Each submission creates one ProcessInstance. While the request waits, a ProcessInstanceWorkitem exists for each assigned approver. When someone responds, the workitem is deleted and a ProcessInstanceStep is added as a permanent record of the action.
A record can have several ProcessInstance records over time. Each rejection or recall ends one instance. A later resubmission creates a new one.
Status values
ProcessInstance.Status is one of Pending, Approved, Rejected, or Removed. Removed means the request was recalled. ProcessInstanceStep.StepStatus can also be Started (the submission itself), Reassigned, or NoResponse.
Querying the data
Query pending requests through the workitem object. The parent relationship gives you the target record.
List<ProcessInstanceWorkitem> pending = [
SELECT Id, ActorId, ProcessInstance.TargetObjectId
FROM ProcessInstanceWorkitem
WHERE ProcessInstance.Status = 'Pending'
];
System.debug(pending.size() + ' pending requests');Query history through the instance, with the child relationship named Steps.
List<ProcessInstance> instances = [
SELECT Id, Status,
(SELECT StepStatus, Actor.Name, Comments, CreatedDate
FROM Steps
ORDER BY CreatedDate)
FROM ProcessInstance
WHERE TargetObjectId = :opportunityId
ORDER BY CreatedDate DESC
];Here opportunityId must be declared as an Id variable in your code, for example in an anonymous Apex block or a method parameter.
Common errors
- Looking for approval status on the record. There is no standard status field. If you need one, create a custom field and set it with a field update action.
- Expecting workitems after a decision. A workitem exists only while a request is pending. For history, query ProcessInstanceStep.
- Assuming one instance per record. Filter on Status or order by CreatedDate to pick the instance you mean.
- Querying a step's Actor when the request was reassigned. Compare Actor with OriginalActor to see who was first assigned and who acted.
- Missing results because of sharing. Users only see approval data for records they can access, so test with the right user.
Summary
- ProcessInstance is the run, ProcessInstanceWorkitem is the open request, and ProcessInstanceStep is the completed action.
- Workitems disappear on a decision, and steps stay.
- Link to the submitted record through TargetObjectId.
Exercise
A finance manager approved an Opportunity yesterday. You query ProcessInstanceWorkitem for that Opportunity and get no rows. Where do you find who approved it and what they commented?
Show answer
The workitem was deleted when the manager responded. Query ProcessInstance where TargetObjectId is the Opportunity Id, and include the child Steps subquery. Read Actor.Name, StepStatus, Comments, and CreatedDate from each ProcessInstanceStep.
Exercise
A record was submitted, recalled, and submitted again. How many ProcessInstance records does it have, and what are their statuses?
Show answer
Two. The first has the status Removed because of the recall. The second has the status Pending until someone approves or rejects it.