0

I'm trying to use invocable apex within a record triggered flow to create a static flag to ensure that code only runs once for each transaction.

When I update more than one record I'm getting error:

FLOW_ELEMENT_ERROR The number of results does not match the number of interviews that were executed in a single bulk execution request.

I've tried adding the results with the invocable method to a static List, to ensure that the number of returned elements is equal to the number of invocations, but I'm still getting the same error.

public class FlowInvocable {
    private static boolean run = true;
    private static List<boolean> boolList = new List<boolean>();
    @InvocableMethod
    public static List<boolean> runOnce() {
        if(run){
            run=false;
            boolList.add(true);
            return boolList;
        }else{
            boolList.add(false);
            return boolList;
        }
    }
    
}

The flow:

Record Triggered Flow Trigger - a record is created or updated, run flow after record is updated

then the only connected element is the invocable action above.

3
  • for what are you using the boolean list? Wouldn't it be enough to return a single boolean? Sep 6, 2020 at 9:40
  • I can't return a single boolean from an invocablemethod - it has to be a list. Originally, I was returning just one element in the list, which errored, when I return multiple elements in the list it also errors in the same way.
    – eaeaoo
    Sep 6, 2020 at 9:43
  • 1
    This error usually occurs when the return value doesn't match the input value. So, are you expecting the invocable method to run only once when a bulk of records are being processed (i.e., 10 DML actions creates 10 flow interviews, but your apex code should execute only once and not for each interview)? If yes, then update the method return type to void and do not return anything (you can use run for controlling the execution/ similar to existing code). I suspect visual flow framework may be expecting 'List of List' in case bulk processing, but not sure without knowing more abt the req.
    – arut
    Sep 6, 2020 at 12:19

3 Answers 3

5

When an InvocableMethod runs, it runs against all interviews in a batch at once. As such, the number of outputs, if any, must match the number of inputs.

In order to fix this error, you need to pass in some value so you can return a list of values all at once.

Something more appropriate would look like:

static Set<Id> recordsProcessed = new Set<Id>();
@InvocableMethod static Boolean[] runOnce(Id[] recordIds) {
  Boolean[] results = new Boolean[0];
  for(Id recordId: recordIds) {
    results.add(recordsProcessed.add(recordId));
  }
  return results;
}

What is happening here is that we call the Set.add method to add the record Id to the Set. If the value already exists in the Set, this method returns false, meaning that the record has already been processed once. If the value does not exist in the Set, the method returns true, meaning that the record has not yet been processed.

0

The above answer by sfdcfox solved the general problem - the specific code I used to solve my issue was:

    private static List<boolean> boolList = new List<boolean>{true};
   @InvocableMethod
    public static List<boolean> runOnce(Id[] recordIds) {
        for(integer i = 1; i < recordIds.Size(); i++) {
            boolList.add(false);
        }
        return boolList;
    }

Which returns a list of the correct size with first element being true and all others false.

0

After running into same Issue und analyzing the Issue this is the aswer:

  1. Issue is mostly caused by Flows and Processes invoking an "Invocable Method".
  2. Flows and Processes REQUIRES the same AMOUNT of Results as Requests which where given to the Invocable Method.
  3. The Return Type must NOT be the same as the Request type.
  4. The Return Type VOID is NOT VALID for Flows and Processes.
  5. Workaround for Users which give back LESS Results than Requests: Input Parameter is List<List> and Response is also LIST<LIST>. The clou is to use always the inner List and add all Requests and Reponses into the inner List. The outer List contains allways the inner List. This results in the same Amount of Requests and Responses (1 List with 1 List)

Last Point Credits go to another User:https://developer.salesforce.com/forums/?id=9062I000000QwROQA0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.