Hello Trailblazers,
In the previous post, we learned: How to pass data from screen flow to lwc in salesforce? In this post we're going to learn, how we can pass data from lightning web component (LWC) to the parent screen flow in salesforce. Let's begin by creating our LWC first.
LWC: createLead
createLead.html
<template> <lightning-card title={title}> <lightning-layout multiple-rows="true"> <lightning-layout-item size="12" padding="around-small"> <lightning-input type="text" name="FirstName" label="First Name" required></lightning-input> </lightning-layout-item> <lightning-layout-item size="12" padding="around-small"> <lightning-input type="text" name="LastName" label="Last Name" required></lightning-input> </lightning-layout-item> <lightning-layout-item size="12" padding="around-small"> <lightning-combobox label="Company" name="Company" placeholder="Select Company" options={companies} required></lightning-combobox> </lightning-layout-item> </lightning-layout> </lightning-card> </template>
createLead.js
import { api, LightningElement } from 'lwc'; export default class CreateLead extends LightningElement { @api title = 'Create Lead'; @api leadRecord = {}; companies = [ { "label": "Apple", "value": "Apple" }, { "label": "Saudi Aramco", "value": "Saudi Aramco" }, { "label": "Microsoft", "value": "Microsoft" }, { "label": "Alphabet (Google)", "value": "Alphabet (Google)" }, { "label": "Amazon", "value": "Amazon" }, { "label": "Tesla", "value": "Tesla" }, { "label": "Berkshire Hathaway", "value": "Berkshire Hathaway" }, { "label": "UnitedHealth", "value": "UnitedHealth" }, { "label": "Johnson & Johnson", "value": "Johnson & Johnson" }, { "label": "Exxon Mobil", "value": "Exxon Mobil" }, { "label": "Visa", "value": "Visa" }, { "label": "Walmart", "value": "Walmart" }, { "label": "JPMorgan Chase", "value": "JPMorgan Chase" }, { "label": "Meta Platforms (Facebook)", "value": "Meta Platforms (Facebook)" }, { "label": "Chevron", "value": "Chevron" }, { "label": "TSMC", "value": "TSMC" }, { "label": "Eli Lilly", "value": "Eli Lilly" }, { "label": "LVMH", "value": "LVMH" }, { "label": "NVIDIA", "value": "NVIDIA" }, { "label": "Procter & Gamble", "value": "Procter & Gamble" }, { "label": "Mastercard", "value": "Mastercard" }, { "label": "Nestlé", "value": "Nestlé" }, { "label": "Tencent", "value": "Tencent" }, { "label": "Home Depot", "value": "Home Depot" }, { "label": "Bank of America", "value": "Bank of America" }, { "label": "Samsung", "value": "Samsung" }, { "label": "Roche", "value": "Roche" }, { "label": "Kweichow Moutai", "value": "Kweichow Moutai" }, { "label": "AbbVie", "value": "AbbVie" }, { "label": "Pfizer", "value": "Pfizer" }, { "label": "Merck", "value": "Merck" }, { "label": "Coca-Cola", "value": "Coca-Cola" }, { "label": "Pepsico", "value": "Pepsico" }, { "label": "Novo Nordisk", "value": "Novo Nordisk" }, { "label": "Costco", "value": "Costco" }, { "label": "Reliance Industries", "value": "Reliance Industries" }, { "label": "ICBC", "value": "ICBC" }, { "label": "Oracle", "value": "Oracle" }, { "label": "Thermo Fisher Scientific", "value": "Thermo Fisher Scientific" }, { "label": "Alibaba", "value": "Alibaba" }, { "label": "Shell", "value": "Shell" }, { "label": "McDonald", "value": "McDonald" }, { "label": "Walt Disney", "value": "Walt Disney" }, { "label": "ASML", "value": "ASML" }, { "label": "Toyota", "value": "Toyota" }, { "label": "Broadcom", "value": "Broadcom" }, { "label": "Danaher", "value": "Danaher" }, { "label": "Cisco", "value": "Cisco" }, { "label": "T-Mobile US", "value": "T-Mobile US" }, { "label": "Astrazeneca", "value": "Astrazeneca" }, { "label": "Wells Fargo", "value": "Wells Fargo" }, { "label": "Accenture", "value": "Accenture" }, { "label": "Novartis", "value": "Novartis" }, { "label": "Abbott Laboratories", "value": "Abbott Laboratories" }, { "label": "L'Oréal", "value": "L'Oréal" }, { "label": "Salesforce", "value": "Salesforce" }, { "label": "ConocoPhillips", "value": "ConocoPhillips" }, { "label": "Bristol-Myers Squibb", "value": "Bristol-Myers Squibb" }, { "label": "Verizon", "value": "Verizon" }, { "label": "China Construction Bank", "value": "China Construction Bank" }, { "label": "Texas Instruments", "value": "Texas Instruments" }, { "label": "Linde", "value": "Linde" }, { "label": "United Parcel Service", "value": "United Parcel Service" }, { "label": "Adobe", "value": "Adobe" }, { "label": "Nextera Energy", "value": "Nextera Energy" }, { "label": "China Mobile", "value": "China Mobile" }, { "label": "Tata Consultancy Services", "value": "Tata Consultancy Services" }, { "label": "Nike", "value": "Nike" }, { "label": "CATL", "value": "CATL" }, { "label": "Agricultural Bank of China", "value": "Agricultural Bank of China" }, { "label": "Amgen", "value": "Amgen" }, { "label": "Comcast", "value": "Comcast" }, { "label": "Morgan Stanley", "value": "Morgan Stanley" }, { "label": "Hermès", "value": "Hermès" }, { "label": "Philip Morris", "value": "Philip Morris" }, { "label": "TotalEnergies", "value": "TotalEnergies" }, { "label": "Charles Schwab", "value": "Charles Schwab" }, { "label": "Raytheon Technologies", "value": "Raytheon Technologies" }, { "label": "QUALCOMM", "value": "QUALCOMM" }, { "label": "Netflix", "value": "Netflix" }, { "label": "BHP Group", "value": "BHP Group" }, { "label": "Royal Bank Of Canada", "value": "Royal Bank Of Canada" }, { "label": "PetroChina", "value": "PetroChina" }, { "label": "Honeywell", "value": "Honeywell" }, { "label": "Elevance Health", "value": "Elevance Health" }, { "label": "AT&T", "value": "AT&T" }, { "label": "CVS Health", "value": "CVS Health" }, { "label": "Lockheed Martin", "value": "Lockheed Martin" }, { "label": "Intuit", "value": "Intuit" }, { "label": "Union Pacific Corporation", "value": "Union Pacific Corporation" }, { "label": "Bank of China", "value": "Bank of China" }, { "label": "IBM", "value": "IBM" }, { "label": "Deere & Company", "value": "Deere & Company" }, { "label": "Toronto Dominion Bank", "value": "Toronto Dominion Bank" }, { "label": "Lowe's Companies", "value": "Lowe's Companies" }, { "label": "Unilever", "value": "Unilever" }, { "label": "HDFC Bank", "value": "HDFC Bank" }, { "label": "Goldman Sachs", "value": "Goldman Sachs" }, { "label": "Intel", "value": "Intel" }, { "label": "Medtronic", "value": "Medtronic" } ]; @api validate() { const inputFields = this.template.querySelectorAll('lightning-input'); const comboBox = this.template.querySelector('lightning-combobox'); const validity = { isValid: true, errorMessage: 'Please fill the required fields!' }; inputFields.forEach(inputField => { if(inputField.checkValidity()) { this.leadRecord[inputField.name] = inputField.value; } else { validity.isValid = false; } }); if(comboBox.checkValidity()) { this.leadRecord[comboBox.name] = comboBox.value; } else { validity.isValid = false; } return validity; } }
validate()
- Validated the input in our lwc using the validate() method and returned the proper output.
- Populated the leadRecord object which will be passed to our screen flow
Out JavaScript part is complete here. Let's move on to the meta file now:
createLead.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>55.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__FlowScreen</target> </targets> <targetConfigs> <targetConfig targets="lightning__FlowScreen"> <property name="title" type="String" label="Title" role="inputOnly"></property> <property name="leadRecord" type="@salesforce/schema/Lead" label="Lead Record" role="outputOnly"></property> </targetConfig> </targetConfigs> </LightningComponentBundle>
- title: The title of our lightning-card and will be passed by our flow to lwc. Notice that it's of type String. The label is Title which is fine and role is inputOnly as we just want to get this variable’s value from the screen flow as an input, we don't want to pass it's value back to the flow if it's updated in our lwc.
- leadRecord: The type of this property is @salesforce/schema/Lead because it'll store the actual salesforce lead record as per the inputs by the user. The label is Lead Record and the role for this property is outputOnly. This is because we'll be passing the value of this variable from LWC to flow. We don't want the admin to set it's value from flow and pass it to LWC.
Screen Flow: Create Lead
Variable: LeadRecord
First of all, let's create a variable named LeadRecord. It should be of data type Record and object name should be Lead as shown below:Screen: Populate Lead Record
Assignment: Assign Default Values
Now we have the lead record coming from LWC with FirstName, LastName and Company populated. Let's assign default values to our LeadSource and Rating as well using the Assignment component, as shown below:Create Records: Create Lead
Finally, we'll use the Create Records component to create a new lead record using our LeadRecord variable as shown below:Add Screen Flow to Homepage
- Search for flow in the left sidebar under Components
- Drag and Drop the flow component on the lightning page
- Choose the Create Lead flow from the Flow Configurations
- Save the page
- Activate the page (if not done before) and set it as org default as we did in our previous tutorial.
No comments:
Post a Comment