Invocable Actions – Calling Flow from Apex with Bulk Data

apexinvocable-methodtriggervisual-workflow

I'm evaluating Mitch Spano's trigger framework that includes the ability to invoke Flows alongside Apex actions (with neat features for ordering, bypassing, etc).

With record-triggered flows, you write the flow as if it's dealing with a single record at a time, and Salesforce handles bulkification of SOQL and DML for you. I'd like to know if that still holds true when I use this framework.

I found the section of code in the trigger framework that invokes Flows. It uses Invocable Actions like this:

public virtual List<Invocable.Action.Result> invokeAction(
  String flowApiName,
  List<Map<String, Object>> inputs
) {
  Invocable.Action action = Invocable.Action.createCustomAction(
    TriggerActionConstants.FLOW_STRING, // "Flow"
    flowApiName // e.g. "My_Flow"
  );
  action.setInvocations(inputs);
  return action.invoke();
}

Based on context, I can see that inputs is a List<Map<String, Object>> that might look something like this (if it were a JSON string):

[
  {
    "record": { "Id": "001AA00001AbcdeAAA", "Name": "Alphabet" }
    "recordPrior": { "Id": "001AA00001AbcdeAAA", "Name": "Google" }
  },
  {
    "record": { "Id": "001AA00001ZyxwvAAA", "Name": "Meta" }
    "recordPrior": { "Id": "001AA00001ZyxwvAAA", "Name": "Facebook" }
  },
]

Documentation on Invocable.Action is sparse, to say the least. So as an example: if my flow includes a single query for more data, and I perform a bulk update on 100+ records, will I incur the "Too many SOQL queries" error (given a governor limit of 100 SOQL queries per transaction)?

Best Answer

This is covered in Flow Bulkification in Transactions, albeit indirectly. There's a note with two bullet points:

  • Unlike Apex actions, legacy Apex actions aren’t bulkified.

The older Process.Plugin is not bulkified, which we can verify on its documentation:

The interface doesn’t support Blob, Collection, sObject, and Time data types, and it doesn’t support bulk operations. Once you implement the interface on a class, the class can be referenced only from flows.

So, be careful if you decide to use an Apex Action (Legacy). They can fail in bulk.

The second bullet notes:

Although Apex actions are bulkified, the flow has no way of knowing what the invoked methods’ operations are. If you want those operations to also be bulkified, make sure the code follows bulkification best practices.

However, you are righteous in your confusion. The InvocableMethod page implies that bulkification is supported:

A list of a primitive data type or a list of lists of a primitive data type – the generic Object type isn’t supported.

Etc.

The list types are the bulkification, and you need a list of lists to support Flow's Collection Variables. This has also been answered in other questions on our network, but I do agree it would be nice if the documentation was clearer on the subject.

Related Topic