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.
tip: 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.
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.
{
name : "varName",
type : "flowDataType",
value : valueToSet
},
{
name : "varName",
type : "flowDataType",
value : [ value1, value2]
}, ...
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.
({
init: function (component) {
// Find the component whose aura:id is "flowData"
var flow = component.find("flowData");
var inputVariables = [
{ name: "numVar", type: "Number", value: 30 },
{ name: "dateColl", type: "String", value: ["2016-10-27", "2017-08-01"] },
// Sets values for fields in the account record (sObject) variable. Id uses
// the value of the component's accountId attribute. Rating uses a string.
{
name: "account",
type: "SObject",
value: {
Id: component.get("v.accountId"),
Rating: "Warm",
},
},
// Set the contact record (sObject) variable to the value of the
// component's contact attribute. We're assuming the attribute contains
// the entire sObject for a contact record.
{ name: "contact", type: "SObject", value: component.get("v.contact") },
];
flow.startFlow("myFlow", inputVariables);
},
});
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.
<aura:component controller="AccountController" >
<aura:attribute name="account" type="Account" />
<aura:handler name="init" value="{!this}" action="{!c.init}"/>
<lightning:flow aura:id="flowData"/>
</aura:component>
public with sharing class AccountController {
@AuraEnabled
public static Account getAccount() {
return [SELECT Id, Name, LastModifiedDate FROM Account
ORDER BY LastModifiedDate DESC LIMIT 1];
}
}
({
init: function (component) {
// Create action to find an account
var action = component.get("c.getAccount");
// Add callback behavior for when response is received
action.setCallback(this, function (response) {
var state = response.getState();
if (state === "SUCCESS") {
// Pass the account data into the component's account attribute
component.set("v.account", response.getReturnValue());
// Find the component whose aura:id is "flowData"
var flow = component.find("flowData");
// Set the account record (sObject) variable to the value of
// the component's account attribute.
var inputVariables = [
{
name: "account",
type: "SObject",
value: component.get("v.account"),
},
];
// In the component whose aura:id is flowData, start your flow
// and initialize the account record (sObject) variable.
// Reference the flow's API name.
flow.startFlow("myFlow", inputVariables);
} else {
console.log("Failed to get account date.");
}
});
// Send action to be executed
$A.enqueueAction(action);
},
});
See Also