0

I have old approval process in which there are 3 approval steps and I created new approval process entirely same as old approval except I removed one approval step and i deactivated old approval and activated new approval.whenever old records which are in locked state I am trying to approve means it was firing old approval process where as when I tested by creating a new record means there are no issues is there any idea how to achieve that old records which are in locked must use new approval process.


public static void sendPOCApproval(set<Id> setOppIds){
        System.debug('sendPOCApproval Line 1');
        List<Opportunity> listOpp =[SELECT Id, ownerid FROM Opportunity WHERE ID IN: setOppIds];
        List<Approval.ProcessSubmitRequest> listOfApprovalRecords = new List<Approval.ProcessSubmitRequest>();
        System.debug('sendPOCApproval Line 2');
        if(!listOpp.isEmpty()){
            System.debug('sendPOCApproval Line 3');
            for(Opportunity opp :  listOpp){
                system.debug('opp.::');
                System.debug('sendPOCApproval Line 4');
                // Create an approval request for the opportunity
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setObjectId(opp.id); 
                System.debug('sendPOCApproval Line 5');
                // Setting submitter of approval
                req.setSubmitterId(opp.OwnerId);
                System.debug('sendPOCApproval Line 6');
                // Submit the record to specific process 
                req.setProcessDefinitionNameOrId('POC_approval');
                System.debug('sendPOCApproval Line 7'); 
                req.setSkipEntryCriteria(true);
                //Adding approval requests to a list
                listOfApprovalRecords.add(req);
                System.debug('sendPOCApproval Line 8');
            }
            System.debug('sendPOCApproval Line 9');
            // Submit the list of approval requests for the opportunity
            if(!listOfApprovalRecords.isEmpty() && !Test.isRunningTest()){
                System.debug('sendPOCApproval Line 10');
                System.debug('listOfApprovalRecords.Size()'+listOfApprovalRecords.size());
                List<Approval.ProcessResult> result = Approval.process(listOfApprovalRecords);
            }
            System.debug('sendPOCApproval Line 11');
        }
        System.debug('sendPOCApproval Line 12');
    }

1 Answer 1

0

This link from apex developer guide shows how to submit requests for approval processing and how to further approve them using apex code. You will first need to recall the approval requests on the records that are using the old approval process and then submit the same records for processing using the new approval process. Some of the records in intermediate stage also need to be handled via code.

To recall an approval process on a record, the code is pretty much the same as what is shown for approval processing in the developer guide, except that the following line of code needs to be replaced request.setAction('Approve'); with request.setAction('Remove');

Edited: Using the same code below you can fetch the opportunity records that have been locked by the old approval process and their current approval step.

List<Id> optyLockedByOldApproval = new List<Id>();
List<Id> lockedOptyIds = new List<Id>();
Map<Id, Boolean> lockedOptyRecords = Approval.isLocked(<Pass the Opportunity Record List here>);
for(Id optyId: lockedOptyRecords.keySet()){
    lockedOptyIds.add(optyId);
}
List<ProcessInstance> piList = [SELECT Id, TargetObjectId, ProcessDefinition.Id, ProcessDefinition.Name, ProcessDefinition.DeveloperName
                                FROM ProcessInstance 
                                WHERE ProcessDefinition.DeveloperName = '<Old Approval Process Name Here>'
                                    AND TargetObjectId IN :lockedOptyIds];
for(ProcessInstance pi: piList){
    optyLockedByOldApproval.add(pi.TargetObjectId);
}

List<ProcessInstanceNode> optyList = [SELECT ProcessInstance.TargetObjectId, Id, ProcessNodeId, ProcessNodeName, NodeStatus 
                                        FROM ProcessInstanceNode 
                                        WHERE ProcessInstance.TargetObjectId = :optyLockedByOldApproval];
5
  • We are not using any method request.set Action method . For reference u can check the code above .There are so many records in locked state.Is there any process to achieve in best possible way
    – Helcyon
    Apr 20, 2020 at 3:11
  • AFAIK, there is no readily available solution to handle this scenario. You could check in the app store if there any apps that help in accomplishing in functionality. IMO, the best way to handle this situation is thru custom apex code. The code you have given is submitting the records for approval. Before you do this, you need to figure out which records have been locked by the previous approval process and handle them.
    – arut
    Apr 20, 2020 at 11:47
  • At a high level, you need to do the following in your apex code (in the same order as mentioned): 1) Identify all the opportunity records locked by the old approval process. Store these records (or their IDs) and their current approval step in a collection. 2) Recall the approval process on these records only. [this is where you will have to use the setAction('remove')] 3) Use your code to submit the records using the new approval process. 4) If some of the records where in the 2nd, 3rd or nth approval step, then you need to write code to get them to the required approval step.
    – arut
    Apr 20, 2020 at 11:55
  • Look at the sample code in the edited section of the solution, that should help you identify the locked records and related information. it might not work as-is and you will have to improvise the code to suit your requirements.
    – arut
    Apr 20, 2020 at 12:19
  • Missed to inform in my earlier comments. While running this code, ensure that the email service is temporarily disabled to avoid generating emails to the submitters and approvers). Or take care of the redundant emails as you see fit.
    – arut
    Apr 21, 2020 at 4:56

You must log in to answer this question.

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