Newer Version Available

This content describes an older version of this product. View Latest

Set Flow Input Variable Values from a Wrapper Lightning Component

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

You can set variables only at the beginning of an interview, and the variables you set must allow input access. For each flow variable, input access is controlled by:

  • The Input/Output Type variable field in the Cloud Flow Designer
  • The isInput field on FlowVariable in the Metadata API

If you reference a variable that doesn’t allow input access, attempts to set the variable are ignored.

Note

For each variable you set, provide the variable's name, type, and value. For type, use the flow data type.

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

Example

This JavaScript controller sets values for a number variable, a date collection variable, and a couple of sObject variables.

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 sObject variable. Id uses the
9         // value of the component's accountId attribute. Rating uses a string.
10         { name : "account", type : "SObject", value: {
11             "Id" : component.get("v.accountId"),
12             "Rating" : "Warm"
13             }
14          },
15          // Set the contact sObject variable to the value of the component's contact
16          // attribute. We're assuming the attribute contains the entire sObject for
17          // a contact record.
18          { name : "contact", type : "SObject", value: component.get("v.contact") }
19          },
20       ];
21       flow.startFlow("myFlow", inputVariables);
22   }
23})

Example

Here's an example of a component that gets an account via an Apex controller. The Apex controller passes the data to the flow's sObject 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 LIMIT 1];
5   }
6}
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         if (state === "SUCCESS") {
9            // Pass the account data into the component's account attribute 
10            component.set("v.account", response.getReturnValue());
11            // Find the component whose aura:id is "flowData"
12            var flow = component.find("flowData");
13            // Set the account sObject variable to the value of the component's 
14            // account attribute.
15            var inputVariables = [
16               {
17                  name : "account",
18                  type : "SObject",
19                  value: component.get("v.account")
20               }
21            ];
22      
23            // In the component whose aura:id is "flowData, start your flow
24            // and initialize the account sObject variable. Reference the flow's
25            // Unique Name.
26            flow.startFlow("myFlow", inputVariables);
27         }
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})