Although Salesforce has the ability to ‘pause’ interactive flows and resume them later, there isn’t any concept of pause for autolaunched flows.

Wouldn’t it be great if we could pause a flow and access its state in Apex?

This would give us the ability to store and retry problematic interviews, or split them into durable parts.

Here’s how to capture the state of any Flow:

Four things need to happen.

  1. Prepare any initial state as a Map<String,Object>
  2. Dynamically instantiate the flow using the CreateInterview method
  3. Coerce the flow into a String to expose all inputs, variables, and outputs
  4. Extract all variable values using the complete set of keys found in the string
 * Dynamically executes a flow and
 * extracts all the state afterwards
Map<String,Object> executeFlow(String flowName, Map<String,Object> variables)
    // 1: Prepare the initial state
    Flow.Interview interview;
    String state = '' + variables;
    if (!Test.isRunningTest())
        // 2: Dynamically instantiate the flow
        interview = Flow.Interview.createInterview(flowName, variables);

        // 3: Coerce to string value
        state += interview;
    Map<String,Object> result = new Map<String,Object>();
    for (String key : state.split('[^A-Za-z0-9_]+'))
        // 4: Extract all variable values
        Object value = variables.get(key);
        if (!Test.isRunningTest()) value = interview.getVariableValue(key);
        if (value != null) result.put(key, value);
    return result;

By integrating interactive AND autolaunched flows, admins can use the tools they love while still interfacing to headless back-end processes. We should be building more on-ramps before building more highways.

Related Posts

Last modified: 4th August 2019