1

I am attempting to create a class that takes a collection of records from a flow and processes any pending approval requests in that collection of records. In my code below I'm getting an error on line 26 where I'm trying to create the list of pending approval requests:

Invalid bind expression type of List for column of type Id

I am using the IN operator as someone suggested prior but I'm still seeing that same error. I would need to keep List<List<String>> as I can't pass a collection variable to a normal list, it has to be a list of list. Any help is appreciated!

Thank you!

global class ProcessApproval implements queueable {
 
    InvocableVariables[] request;
    public ProcessApproval(InvocableVariables[] request) {
        this.request = request;
    }
    @InvocableMethod    
    public static void ProcessApprovalMethod(List <InvocableVariables> request){
        System.enqueueJob(new ProcessApproval(request));
    }
    public void execute(QueueableContext context) {
        
        //invocable variables 
        Id recordToProcess = request[0].recordToProcess;
        String actionToPerform = request[0].actionToPerform;
        List<List<String>> recordIDs = request[0].recordIDs;
        
        
        //try/catch block for error handling
        try {
            
            List<ProcessInstanceWorkitem> workItems = [
                SELECT Id, ProcessInstanceId 
                FROM ProcessInstanceWorkitem 
                WHERE ProcessInstance.TargetObjectId IN :recordIDs
            ];
            List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
            for(ProcessInstanceWorkitem workItem : workItems){
                Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                req.setWorkitemId(workItem.Id);
               
                req.setAction(actionToPerform);
                req.setComments('Processed via backend automation.');
                requests.add(req);
            }
            Approval.ProcessResult[] processResults = Approval.process(requests);
      
        }
        catch(Exception ex) {
            system.debug('Exception has occured' + ex.getMessage());
            
        }
    }    
    global class InvocableVariables{
        
        //Source record ID
        @InvocableVariable public Id recordToProcess;
        //Approve/Reject/Recall action
        @InvocableVariable public String actionToPerform;
        //List of record IDs to process
        @InvocableVariable public List<List<String>> recordIDs;
        
    }
}

1 Answer 1

3

To start with

WHERE ProcessInstance.TargetObjectId IN :recordIDs

But recordIds is defined as

List<List<String>> recordIDs = request[0].recordIDs;

The bind variable needs to be a simple list, not a list of lists

But, you have bigger issues..

Issue 1

Your Invocable Process acts as a bulkified processor of all flow interviews in the same transaction. Hence, as you have coded, it receives a List<InvocableVariables>

But InvocableVariables is defined as:

global class InvocableVariables{
    
    //Source record ID
    @InvocableVariable public Id recordToProcess;
    //Approve/Reject/Recall action
    @InvocableVariable public String actionToPerform;
    //List of record IDs to process
    @InvocableVariable public List<List<String>> recordIDs;
    
}

recordIds should only be List<String>, not List<List<String>> because each instance of InvocableVariables represents one flow interview and you are passing in that single flow interview a collectionVariable that is a scalar list.

Issue 2

Since the invocable is handing off work to the queueable, the queueable needs to go through all of the InvocableVariables in request, not just item [0]

Issue 3

Naming conventions

  • The inner class should be named singular - something like Request
  • The invocable argument, should be named as a plural as refers to a collection of Request, so, name it requests (same for the class property request
  • Then, you are redeclaring requests to mean Approval.ProcessWorkitemRequests - I would call this variable pwiRequests

Collections should be named as plurals, singletons named as non-plural. This goes a long way in solving compile errors

1
  • Thank you so much! This helped immensely
    – Jas
    Nov 10, 2021 at 20:00

You must log in to answer this question.

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