I have a quick question here.

How can identify the records that are related to a Approval Process. I fetched all the records of Process Instance but there I got Target Object ID.

I have three approval process on a object.Two of them have been deactivated. I have to find the Approval records that are link to these two approval process.

2 Answers 2


Use the below query to get the records for Obsolete Approval Process

Select TargetObjectId, Status, ProcessDefinitionId, LastActorId, IsDeleted, Id, ElapsedTimeInMinutes, ElapsedTimeInHours, ElapsedTimeInDays, CompletedDate From ProcessInstance p where ProcessDefinitionId IN ('Pass IDs of the Deactivated Approval Process here').

This will give you the ids of the records for these approval process.

  • And if you try to delete them it will give you an error, seems like approval process are not deletable which is insane
    – ensir
    Commented Oct 6, 2022 at 18:22

I wrote an Apex script to prepare content for a destructive package:

List<ProcessDefinition> inactiveApprovalProcesses = [
    SELECT DeveloperName, TableEnumOrId
    FROM ProcessDefinition
    WHERE State IN ('Inactive', 'Obsolete')
    AND TableEnumOrId <> 'KnowledgeArticleVersion'

List<AggregateResult> results = [
    SELECT ProcessDefinition.DeveloperName
    FROM ProcessInstance
    WHERE ProcessDefinitionId IN :inactiveApprovalProcesses
    GROUP BY ProcessDefinition.DeveloperName

Map<String, String> inactiveApprovalProcessNames = new Map<String, String>();
for (ProcessDefinition pd : inactiveApprovalProcesses) {
    inactiveApprovalProcessNames.put(pd.DeveloperName, pd.TableEnumOrId);

Set<String> unableToDelete = new Set<String>();
for (AggregateResult result : results) {
    unableToDelete.add((String) result.get('DeveloperName'));

// Remove anything that has been used


System.debug('These approval processes can be deleted:\n' +

String metadataString = '\n<types>\n';
for (String key: inactiveApprovalProcessNames.keySet()) {
    metadataString +='        <members>' +
        inactiveApprovalProcessNames.get(key) + '.' + key + '</members>\n';
metadataString += '        <name>ApprovalProcess</name>\n</types>\n';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .