Hello Trailblazers,
You might have come across the below blog posts published by me in the past:
- How to pass data from lwc to screen flow in salesforce?
- How to pass data from screen flow to lwc in salesforce?
You might be thinking, what the heck are we doing in this post then???
Give me a moment to clarify: In the above posts, we actually embedded a LWC component within a screen flow and passed data to it/received data from it. However, in today's post, we're going to do exactly the opposite. We're going to embed a screen flow within a LWC component and pass data to screen flow, receive data from screen flow. We're going to use lightning-flow lwc component provided by salesforce in this tutorial. So, without spending much time on discussion. Let's begin!
First of all we're going to create a very basic LWC component called flowContainer which will have our flow. We're going to embed this component on the homepage just like other demo components which we created. The content of .meta-xml file for the same is provided below:
flowContainer.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>59.0</apiVersion> <isExposed>true</isExposed> <masterLabel>Flow Container</masterLabel> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>
Screen Flow - Duplicate Contacts
- Get the list of contact ids from flowContainer LWC
- Create a new account record
- Query the existing contact records using their ids, create a copy of those, tag them to the new account record and insert them in salesforce
- Pass the new account record id to flowContainer LWC



flowContainer.html
<template> <template lwc:if={showFlow}> <lightning-flow flow-api-name="DuplicateContacts" flow-input-variables={inputVariables} onstatuschange={handleStatusChange}> </lightning-flow> </template> <template lwc:else> <lightning-button label="Duplicate Contacts" onclick={launchFlow}></lightning-button> </template> </template>
- flow-api-name: This should be the API name of the flow. For our flow, it's DuplicateContacts.
- flow-input-variables: This refers to the array of input variables i.e. the flow variables whose values we're going to pass from this LWC component.
- onstatuschange: We're capturing the statuschange event here and calling our handleStatusChange method. statuschange event is fired whenever the status of the flow is changed. For example: when the flow is started/paused/finished etc.
flowContainer.js
import { LightningElement } from 'lwc'; import { NavigationMixin } from 'lightning/navigation'; export default class FlowContainer extends NavigationMixin(LightningElement) { // * Boolean to display/hide flow showFlow = false; // * Ids of contact records to be passed to flow contactIds = [ '003H3000001l11BIAQ', '003H3000001l11AIAQ', '003H3000001l112IAA' ]; // * Input variables to pass to flow from LWC inputVariables = [ { name: 'ids', type: 'String', value: this.contactIds } ] /** * @description This method is used to launch the flow from lwc */ launchFlow() { this.showFlow = true; } /** * * @param {object} event status change event - received when flow state is changed * @description This method will be called whenever the state of the flow is updated */ handleStatusChange(event) { if(event.detail.status === 'FINISHED') { let accountVariable = event.detail.outputVariables?.find( outputVariable => outputVariable.name === 'NewAccount' ); this.navigateToRecordPage(accountVariable.value.Id); } } /** * * @param {string} recordId Id of the record * @description This method is used to navigate the current user to the detail page of the record whose id is passed as parameter */ navigateToRecordPage(recordId) { this[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { recordId: recordId, actionName: 'view', }, }); } }
- The name should be the name of the variable as defined in our flow. For our entry it's ids as we defined ids variable in the flow which will store the contact ids.
- The type is the data type of the variable. In our case it's String (Text in flow)
- The value should be the value of the variable defined in our flow. For our scenario, we want to pass the list of contact ids as value to our ids variable. Therefore, we've referred to our contactIds variable here which is defined above in the js and passed that as the value for our ids flow variable.
- launchFlow(): This method will be called when we click Duplicate Contacts button in our HTML. It'll set showFlow attribute to true which will automatically hide the button and will show the screen flow in our LWC. In a way we can say, this method is used to launch our screen flow.
- handleStatusChange(event): This method will be called whenever the flow status is updated. It's binded to statuschange event of our lightning-flow component. Inside this method, we need to check: If the flow is finished, we need to redirect the user to the detail page of newly created account record. We're receiving event inside this method as a parameter so, we're basically checking here if: event.detail.status === FINISHED i.e. if the flow is finished, we're referring to the outputVariables in our flow using event.detail.outputVariables to get the newly created account record.
Remember that Available for output checkbox which we checked in our NewAccount variable inside the flow? That was marked so that it's value can be accessed outside the flow. Therefore, that variable will be received in the array of our ouputVariables accessed through event.detail.outputVariables.
I'm sharing the NewAccount flow variable below again for your reference:
As you can notice, we marked this as Available for output so that we can receive the value of this variable outside the flow i.e. in our case - inside our flowContainer lwc component. If you log event.detail.outputVariables, you'll get an array as shown below:
As you can see above, there is only a single entry in this array and that is for our NewAccount variable. It's name property is having a value as NewAccount, it's objectType is Account. This variable is not a collection so isCollection is false. It's coming from flow DuplicateContacts so the flow name is the same and it's dataType is SOBJECT as it stores the value of an sObject record. Moving onto the value property, as we only populated the name of the account while creating the record from flow, it's value is an object having only two properties: Id and Name.
Moving back to our code, we are using find() method to find the outputVariable from our outputVariables array where name = NewAccount. This will return us the first entry of our array. We're storing this in a js variable named accountVariable. Finally, we're accessing this account record's id using accountVariable.value.Id and passing it to our navigateToRecordPage() method which will navigate the current user to the new account's record page.
Note: This is the point where we're receiving data from flow in our LWC component. The variables present in our flow which are marked as available for output, will be received in our LWC component inside outputVariables array under our event. - navigateToRecordPage(recordId): This method is used to navigate the current user to the detail page of the record whose record id is passed as a parameter. This method is called from handleStatusChange() method. We're using NavigationMixin.Navigate to navigate to the standard record page and we're passing recordId as the value to our recordId attribute.