2

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

1

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.

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

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'));
}

//System.debug(JSON.serializePretty(unableToDelete));
// Remove anything that has been used

inactiveApprovalProcessNames.keySet().removeAll(unableToDelete);

System.debug('These approval processes can be deleted:\n' +
    JSON.serializePretty(inactiveApprovalProcessNames));

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';
System.debug(metadataString);

You must log in to answer this question.

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