html code

How to trigger a flow from apex in Salesforce

In this blog, we will discuss on how to invoke a lightning Auto launched flow from the apex class.

1. It works only with autolaunched Flow.

2. Use the start method in the Flow.Interview class to launch an autolaunched flow or user provisioning flow from Apex.

3. In this example, we will use a visualforce page where flow will be invoked on a button click.

4. We assume you have a created a flow, please use it further.

VISUALFORCE PAGE

<apex:page controller="FlowController">
   <apex:outputLabel id="text">v1 = {!output}</apex:outputLabel>

   <apex:form >
      value : <apex:inputText value="{!output}"/>
      <apex:commandButton action="{!start}" value="Start" reRender="text"/>
   </apex:form>
</apex:page>

APEX CONTROLLER

public class FlowController {
            
   //Instance of the Flow
   public Flow.Interview.doubler myFlow {get; set;}
   public Double value {get; set;}

   public Double getOutput() {
      if (myFlow == null) return null;
      return (Double)(myFlow.getVariableValue('v1'));
   }

   public void start() {
      Map<String, Object> myMap = new Map<String, Object>();
      myMap.put('v1', input);
      myFlow = new Flow.Interview.doubler(myMap);
      myFlow.start();
   }
}