function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bohemianguy100bohemianguy100 

approval process trigger

I have an approval process on a custom object that when submitted, the user submitting the record manually selects the approver in each step.  Based on the requirements, the object is set to private.  So, I need to write a trigger that will manually create read-only access to the approvers.  I can create the read-only access sharing using apex code, but where I'm having trouble is with the ApprovalProcess classes.

 

I only want the trigger to fire when the record has been submitted for approval and the approval step is going to an approver.

 

It looks like I need to use the ProcessWorkItem and ProcessRequest classes.  I can use the getNextApproverIds method to get the user Id of the approver to set the read-only access, but how do I identify if the record has been submitted for approval and is in a step to be approved?

 

Does anyone have a code sample?  I've googled and found some examples of how to submit the record for approval using Apex, but I didn't find anything that shows how to identify the record was submitted for approval and getting the user Id of the next approver.

 

Thanks.

colemabcolemab

This link has code that queries and finds only items in a pending status.  Per haps you could look at the SOQL in it and get what you need?

Starz26Starz26

Here is an example I use to cancel all approvals currently pending for a particular record. Maybe it will help.

 

 

Approval.ProcessWorkitemRequest[] prWkItems = New Approval.ProcessWorkItemRequest[]{};
            //Reject the record
            ProcessInstance[] pi = [Select ID, Status, TargetObject.Name, 
                (SELECT Id, ActorId, ProcessInstanceId FROM Workitems),
                (SELECT Id, StepStatus, Comments FROM Steps) From ProcessInstance 
                Where TargetObjectID IN :tbuApprovals AND Status = 'Pending'];
            
      
            
            for(ProcessInstance instance : pi){
      
                for(ProcessInstanceWorkItem workItem : instance.WorkItems){
                  
                  
                    Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
                    
                    prWkItem.setWorkItemID(workItem.id);
                    prWkItem.setComments('Request Canceled by User: ' + userInfo.getName());
                    prWkItem.setAction('Reject');
                    prWkItems.add(prWkItem);
                  
                }
            }          
            
            if(!prWkItems.isEmpty()){
                shouldTriggerRun.stopFAR();
                Approval.ProcessResult[] appResult = Approval.process(prWkItems);
                shouldTriggerRun.allowFAR();
            }

 

Jerry.ax1540Jerry.ax1540

Hi starz 

Thanks for your solution its working