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.
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 : valueToSet5},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 uses9 // 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 the19 // component's contact attribute. We're assuming the attribute contains20 // 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.
1public with sharing class AccountController{2 @AuraEnabled3 public static Account getAccount(){4 return[SELECT Id, Name, LastModifiedDate FROM Account5 ORDER BY LastModifiedDate DESC LIMIT 1];6}7}
1({2 init: function(component){3 // Create action to find an account4 var action = component.get("c.getAccount");56 // Add callback behavior for when response is received7 action.setCallback(this, function(response){8 var state = response.getState();9 if(state === "SUCCESS"){10 // Pass the account data into the component's account attribute11 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 of15 // the component's account attribute.16 var inputVariables = [17{18 name: "account",19 type: "SObject",20 value: component.get("v.account"),21},22];2324 // In the component whose aura:id is flowData, start your flow25 // 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});3233 // Send action to be executed34 $A.enqueueAction(action);35},36});