Interview Class

The Flow.Interview class provides advanced controller access to flows and the ability to start a flow.

Namespace

Flow

Usage

SOQL and DML limits apply during flow execution. See Apex Governor Limits that Affect Flows in the Cloud Flow Designer Guide.

To create an Interview object, you have two options.
  • Create the object directly in your class by using:
    • No namespace: Flow.Interview.flowName
    • Namespace: Flow.Interview.namespace.flowName
  • Create the object dynamically by using createInterview()
Note

Note

We recommend only using createInterview() if you need to reuse your method or class. Using createInterview() has these drawbacks.

  • If you package a class that uses createInterview(), you have to add the associated flow manually.
  • If you delete a flow, Salesforce doesn't check if it's referenced with createInterview().

Examples: Starting Flow Interviews

The following examples are all sample controllers that start an interview for the flow from the Build a Discount Calculator project on Trailhead. Each shows a different permutation, based on:
  • Whether the interview is created statically, with Flow.Interview.myFlow, or dynamically, with createInterview().
  • Whether the flow is managed or local.
Interview Created Statically for a Local Flow
{
  Map<String, Object> inputs = new Map<String, Object>();
  inputs.put('AccountID', myAccount);
  inputs.put('OpportunityID', myOppty);
  
  Flow.Interview.Calculate_discounts myFlow = 
    new Flow.Interview.Calculate_discounts(inputs);
  myFlow.start();
}
Interview Created Dynamically for a Local Flow
public void callFlow(String flowName, Map <String, Object> inputs) {
  Flow.Interview myFlow = Flow.Interview.createInterview(flowName, inputs);
  myFlow.start();
}
Interview Created Statically for a Managed Flow
{
  Map<String, Object> inputs = new Map<String, Object>();
  inputs.put('AccountID', myAccount);
  inputs.put('OpportunityID', myOppty);
  
  Flow.Interview.myNamespace.Calculate_discounts myFlow = 
    new Flow.Interview.myNamespace.Calculate_discounts(inputs);
  myFlow.start();
}
Interview Created Dynamically for a Managed Flow
public void callFlow(String namespace, String flowName, Map <String, Object> inputs) {
  Flow.Interview myFlow = Flow.Interview.createInterview(namespace, flowName, inputs);
  myFlow.start();
}

Example: Getting Variable Values

This sample uses the getVariableValue method to obtain breadcrumb (navigation) information from a flow. If that flow contains subflow elements, and each of the referenced flows also contains a vaBreadCrumb variable, you can provide users with breadcrumbs regardless of which flow the interview is running.
public class SampleController {

   //Instance of the flow
   public Flow.Interview.Flow_Template_Gallery myFlow {get; set;}

   public String getBreadCrumb() {
      String aBreadCrumb;
      if (myFlow==null) { return 'Home';}
      else aBreadCrumb = (String) myFlow.getVariableValue('vaBreadCrumb');

      return(aBreadCrumb==null ? 'Home': aBreadCrumb);

   }
}

Interview Methods

The following are instance methods for Interview.

createInterview(namespace, flowName, inputVariables)

Creates an interview for a namespaced flow.

Signature

public static Flow.Interview createInterview(String namespace, String flowName, Map<String,ANY> inputVariables)

Parameters

namespace
Type: String
The flow’s namespace.
flowName
Type: String
The flow’s unique name.
inputVariables
Type: Map<String,Object>
Initial values for the flow’s input variables.

Return Value

Type: Flow.Interview

Usage

Use this method to dynamically create a Flow.Interview object for the start() method.

How you get output variable values from an interview depends on the type of the Apex variable where you're storing the interview.
  • If the variable is cast to a specific flow, you can use myFlow.myVar to access a variable, where myVar is the name of the variable.
    system.debug('My Output Variable: ' + myFlow.varName);
  • If the variable is of type Flow.Interview but not cast to a specific flow, you must use getVariableValue() to access the flow's variables.
    system.debug('My Output Variable: ' + myFlow.getVariableValue('varName'));

If the flow doesn't exist in the current org, a TypeException is thrown.

createInterview(flowName, inputVariables)

Creates an interview for a flow.

Signature

public static Flow.Interview createInterview(String flowName, Map<String,Object> inputVariables)

Parameters

flowName
Type: String
The flow’s unique name.
inputVariables
Type: Map<String,Object>
Initial values for the flow’s input variables.

Return Value

Type: Flow.Interview

Usage

Use this method to dynamically create a Flow.Interview object for the start() method.

How you get output variable values from an interview depends on the type of the Apex variable where you're storing the interview.
  • If the variable is cast to a specific flow, you can use myFlow.myVar to access a variable, where myVar is the name of the variable.
    system.debug('My Output Variable: ' + myFlow.varName);
  • If the variable is of type Flow.Interview but not cast to a specific flow, you must use getVariableValue() to access the flow's variables.
    system.debug('My Output Variable: ' + myFlow.getVariableValue('varName'));

If the flow doesn't exist in the current org, a TypeException is thrown.

getVariableValue(variableName)

Returns the value of the specified flow variable. The flow variable can be in the flow embedded in the Visualforce page, or in a separate flow that is called by a subflow element.

Signature

public Object getVariableValue(String variableName)

Parameters

variableName
Type: String
Specifies the unique name of the flow variable.

Return Value

Type: Object

Usage

The returned variable value comes from whichever flow the interview is running. If the specified variable can’t be found in that flow, the method returns null.

This method checks for the existence of the variable at run time only, not at compile time.

start()

Starts an instance (interview) for an autolaunched or user provisioning flow.

Signature

public Void start()

Return Value

Type: Void

Usage

This method can be used only with flows that have one of these types.
  • Autolaunched Flow
  • User Provisioning Flow
For details, see “Flow Types” in the Cloud Flow Designer Guide.

When a flow user invokes an autolaunched flow, the active flow version is run. If there’s no active version, the latest version is run. When a flow admin invokes a flow, the latest version is always run.