Monday, July 22, 2019

Tips and Tricks : Approving Approval Process in Test Class

Snippet of test class that replicate how to approve record in Approval Process.d


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
      //Step 1: let say you a test data that you want to be approved      
   MyCustomObject__c bid=[select Id from MyCustomObject__c Where Name=:strName];
  
       //Step 2:  Create an approval request for the bid
      Approval.ProcessSubmitRequest req =     new Approval.ProcessSubmitRequest();
      req.setComments('Submitting request for approval via form');
      req.setObjectId(bid.Id);
     
      //Step 3:  Submit on behalf of a specific submitter
      req.setSubmitterId(UserInfo.getUserId());
   
      //Step 4:  Submit the record to specific process and skip the criteria evaluation
      req.setProcessDefinitionNameOrId('Qualification_Approval');
      req.setSkipEntryCriteria(true);

      //Step 5:  Submit the approval request for the bid
      Approval.ProcessResult result = Approval.process(req);

      //Step 6:  Instantiate the new ProcessWorkitemRequest object and populate it
      Approval.ProcessWorkitemRequest req =
      new Approval.ProcessWorkitemRequest();
      req.setComments('Approving request.');
   req.setAction('Approve');

      //Step 7 : To check the next process after approval being submitted/approved etc.(basically next stage)  
   ProcessInstanceWorkitem pItem = [Select Id from ProcessInstanceWorkitem  where ProcessInstance.TargetObjectId =: bid.id];
      
   // Use the ID from the newly created item to specify the item to be worked
      req.setWorkitemId(pItem.Id);

      //Step 8 : result of process
      Approval.ProcessResult result = Approval.process(req);

Let try to breakout the API one by one

All of above are using Approval API.

No comments:

Post a Comment