Loading
Salesforce now sends email only from verified domains. Read More
Automate Your Business Processes
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Start a Flow from Apex

          Start a Flow from Apex

          Flows started from Apex can include complex business logic and enhanced error handling, and can integrate with external systems more effectively. This approach promotes reusability by encapsulating logic within flows that can be called as needed. It also provides greater control over execution context and order of operations. The combination of Apex and flow helps you create more powerful and flexible apps that are easier to maintain.

          Note
          Note Starting a flow from Apex as a flow admin uses the latest version of the flow, regardless of activation status.

          You can use the Invocable.Action class or the Flow.Interview class to start a flow from Apex. Consider the differences between the two classes.

          • If you use the Invocable.Action class to run flows, the flows run in batches with bulkification, but you can only reference flows dynamically. You provide a string value for the flow name that doesn't provide referential integrity. This approach can be better when you want to run arbitrary flows, but not when you're calling a specific flow.
          • To get referential integrity, use the Flow.Interview class to reference flows statically. You can’t package or deploy the Apex without the flow existing in either the target org, the deployment, or the package. However, the Flow.Interview class can't run in batches by using bulkification.

          Salesforce Object Query Language (SOQL) and Data Manipulation Language (DML) limits apply during flow execution. See Per-Transaction Flow Limits in Salesforce Help.

          In the examples, consider this sample flow:

          • Flow API name: MyFunFlow
          • The flow has two text variables that are available for input:
            • inputA
            • inputB
          • The flow has one text variable that’s available for output:
            • outputVariable
          • The flow has an Assignment element that concatenates the values of inputA and inputB and assigns the result to outputVariable

          Running a Single Autolaunched Flow from Apex with the Invocable.Action Class

          To run a flow by using Invocable.Action, create an instance of the Invocable.Action class by using the createCustomAction() method. Set the input variable values for one or more instances of the flow you want to run, and then run the invoke() method. The invoke() method returns an object that can read any errors or outputs the flow provides.

          With this approach, you can run multiple instances of the same flow for different sets of inputs.

          /* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
          Map<String,Object> flowInputVariables = new Map<String,Object>();
          flowInputVariables.put('inputA','hello');
          flowInputVariables.put('inputB','world');
          
          /* Put the input map(s) into a List. Each item in this list represents a flow interview that's invoked together in a single batch run that uses flow bulkification. You must use a list, even if you want to run just one instance of the flow. */
          List<Map<String,Object>> flowInputs = new List<Map<String,Object>{ flowInputVariables };
          
          /* Initialize the flow as an action with the Action Type 'flow' and the API name of the flow */
          Invocable.Action flowAction = Invocable.Action.createCustomAction('flow','MyFunFlow');
          
          /* Use the input parameters to prepare the invocations that will be invoked */
          flowAction.setInvocations(flowInputVariables);
          
          /* Invoke the flow, assigning the results to a list of Invocable.Action.Result */
          List<Invocable.Action.Result> flowResults = flowAction.invoke();
          
          /* Read the results and outputs of the invocation */
          for (Invocable.Action.Result result : flowResults) {
          	if (result.isSuccess() == false) {
          		// This invocation failed! read what the errors are with getErrors()
          		List<Invocable.Action.Error> errors = result.getErrors();
          	} else {
          		// Success! Read the outputs with getOutputParameters()
          		Map<String,Object> outputValues = result.getOutputParameters();
          		String myFlowOutput = (String)outputValues.get('outputVariable');
          		System.debug('Result: ' + myFlowOutput);
          	}
          }
          

          The debug logs show:

          > Result: Hello World

          Running Multiple Instances of an Autolaunched Flow with the Invocable.Action Class and a Single invoke()

          /* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value. */
          Map<String,Object> flowInput1 = new Map<String,Object>();
          flowInput1.put('inputA','hello');
          flowInput1.put('inputB','world');
          
          Map<String,Object> flowInput2 = new Map<String,Object>();
          flowInput2.put('inputA','foo');
          flowInput2.put('inputB','bar');
          
          /* Put the input map(s) into a list. Each item in this list represents a flow interview invoked together in a single batch run that uses  flow bulkification . You must use a list, even if you want to run one instance of the flow only. */
          List<Map<String,Object>> flowInputs = new List<Map<String,Object>();
          flowInputs.add(flowInput1);
          flowInputs.add(flowInput2);
          
          /* Initialize the flow as an action with the Action Type 'flow' and the API name of the flow */
          Invocable.Action flowAction = Invocable.Action.createCustomAction('flow','MyFunFlow');
          
          /* Use the input parameters to prepare the invocations that will be invoked */
          flowAction.setInvocations(flowInputs);
          
          /* Invoke the flow, setting the result to a list of Invocable.Action.Result */
          List<Invocable.Action.Result> flowResults = flowAction.invoke();
          
          /* Read the results and outputs of the invocation */
          for (Invocable.Action.Result result : flowResults) {
          	if (result.isSuccess() == false) {
          		// This invocation failed! Read what the errors are with getErrors()
          		List<Invocable.Action.Error> errors = result.getErrors();
          	} else {
          		// Success! Read the outputs with getOutputParameters()
          		Map<String,Object> outputValues = result.getOutputParameters();
          		String myFlowOutput = (String)outputValues.get('outputVariable');
          		System.debug('Result: ' + myFlowOutput);
          	}
          }
          

          The debug logs show:

          > Result: hello world > Result: foo bar
          > Result: foo bar

          Running a Single Autolaunched Flow from Apex with the Flow.Interview Class

          To run a flow by using the Flow.Interview class, create an instance of the Flow.Interview by using the createInterview() method, or a static instance of Flow.Interview.FlowApiName. Set the input variables, and then run the start() method. After running the start() method, you can read the resulting values of any output variables by using the getVariableValue() method on the instance of the Flow.Interview.

          You can only run one Flow.Interview at a time with either the dynamic or static approaches.

          Dynamically Run the Flow with Flow.Interview.createInterview()

          /* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
          Map<String,Object> flowInputVariables = new Map<String,Object>();
          flowInputVariables.put('inputA','hello');
          flowInputVariables.put('inputB','world');
          
          /* Initialize the flow using the API name of the flow and the list of inputs you've defined */
          Flow.Interview myFlow = Flow.Interview.createInterview('MyFunFlow',flowInputVariables);
          
          /* Invoke the flow, setting the result to a list of Invocable.Action.Result */
          myFlow.start();
          
          /* Read the results and outputs of the invocation */
          String myFlowOutput = (String)myFlow.getVariableValue('outputVariable');
          
          System.debug('Result: ' + myFlowOutput);
          Result: hello world

          Statically Run the Flow with Flow.Interview.FlowApiName

          /* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
          Map<String,Object> flowInputVariables = new Map<String,Object>();
          flowInputVariables.put('inputA','hello');
          flowInputVariables.put('inputB','world');
          
          /* Initialize the flow using the API name of the flow and the list of inputs you've defined. By using this mechanism, you are creating a static reference to the flow, meaning any deployment or packaging of this Apex  requires the flow to exist in the target org,deployment, or package */
          Flow.Interview myFlow = new Flow.Interview.MyFunFlow(flowInputVariables);
          
          /* Invoke the flow, setting the result to a list of Invocable.Action.Result */
          myFlow.start();
          
          /* Read the results and outputs of the invocation */
          String myFlowOutput = (String)myFlow.getVariableValue('outputVariable');
          
          System.debug('Result: ' + myFlowOutput);
          > Result: hello world

          Running Screen Flows in Custom Code

          From Visualforce: see Embed a Flow in a Visualforce Page

          From a custom Lightning component: see Embed a Flow in a Custom Aura Component

           
          Loading
          Salesforce Help | Article