Set Flow Input Variable Values from a Wrapper Aura Component

When you embed a flow in a custom Aura component, give the flow more context by initializing its variables. In the component’s controller, create a list of maps, then pass that list to the startFlow method.

We recommend using Lightning web components because they perform better and provide the latest functionality. See Embed a Flow in a Custom Lightning Web Component.

Tip

You can set variables only at the beginning of an interview, and the variables you set must allow input access. If you reference a variable that doesn’t allow input access, attempts to set the variable are ignored.

For each variable you set, provide the variable’s name, type, and value. For type, use the API name for the flow data type. For example, for a record variable use SObject, and for a text variable use String.

1{
2     name : "varName",
3     type : "flowDataType",
4     value : valueToSet
5},
6{
7     name : "varName",
8     type : "flowDataType",
9     value : [ value1, value2]
10}, ...

Example: Set Values for Variables 

In this example, this JavaScript controller sets values for a number variable, a date collection variable, and a couple of record variables. The Record data type in Flow Builder corresponds to SObject here.

1({
2  init: function (component) {
3    // Find the component whose aura:id is "flowData"
4    var flow = component.find("flowData");
5    var inputVariables = [
6      { name: "numVar", type: "Number", value: 30 },
7      { name: "dateColl", type: "String", value: ["2016-10-27", "2017-08-01"] },
8      // Sets values for fields in the account record (sObject) variable. Id uses
9      // the value of the component's accountId attribute. Rating uses a string.
10      {
11        name: "account",
12        type: "SObject",
13        value: {
14          Id: component.get("v.accountId"),
15          Rating: "Warm",
16        },
17      },
18      // Set the contact record (sObject) variable to the value of the
19      // component's contact attribute. We're assuming the attribute contains
20      // the entire sObject for a contact record.
21      { name: "contact", type: "SObject", value: component.get("v.contact") },
22    ];
23    flow.startFlow("myFlow", inputVariables);
24  },
25});

Example: Retrieve the Most Recently Modified Account via an Apex Controller 

In this example, the Apex controller passes the data to the flow’s record variable through the JavaScript controller.

1<aura:component controller="AccountController" >
2   <aura:attribute name="account" type="Account" />
3   <aura:handler name="init" value="{!this}" action="{!c.init}"/>
4   <lightning:flow aura:id="flowData"/>
5</aura:component>
1public with sharing class AccountController {
2    @AuraEnabled
3    public static Account getAccount() {
4       return [SELECT Id, Name, LastModifiedDate FROM Account
5       ORDER BY LastModifiedDate DESC LIMIT 1];
6    }
7 }
1({
2  init: function (component) {
3    // Create action to find an account
4    var action = component.get("c.getAccount");
5
6    // Add callback behavior for when response is received
7    action.setCallback(this, function (response) {
8      var state = response.getState();
9      if (state === "SUCCESS") {
10        // Pass the account data into the component's account attribute
11        component.set("v.account", response.getReturnValue());
12        // Find the component whose aura:id is "flowData"
13        var flow = component.find("flowData");
14        // Set the account record (sObject) variable to the value of
15        // the component's account attribute.
16        var inputVariables = [
17          {
18            name: "account",
19            type: "SObject",
20            value: component.get("v.account"),
21          },
22        ];
23
24        // In the component whose aura:id is flowData, start your flow
25        // and initialize the account record (sObject) variable.
26        // Reference the flow's API name.
27        flow.startFlow("myFlow", inputVariables);
28      } else {
29        console.log("Failed to get account date.");
30      }
31    });
32
33    // Send action to be executed
34    $A.enqueueAction(action);
35  },
36});

See Also