• +91 9311 5511 17
  • info@websharpmarketing.com
Web Sharp Marketing Web Sharp Marketing
Get A Quote
  • Home
  • About Us
  • Our Services
    • Full Stack Development
    • Salesforce Training
    • Salesforce Solutions
    • Data Migration and Processing
    • Code Review, QA and Support
    • Website Designing
  • Blog
  • Contact Us
  • March 5, 2024
  • WSM
  • 0 Comments
  • 221 Views
  • 1 Likes
  • Development, Flows, LWC

How to Invoke a Flow from a Lightning Web Component

Use Case

First, we create a contact record from the screen flow then we will fire a toast message and navigate to the contact to record detail page.

How to Fire a Toast Message in Flow?

Will start first with creating a screen flow, where the user enters some inputs to create a contact.

Go to Flows in Setup and choose Screen Flow. To create a contact I’m choosing Name, Email, and Description as input parameters from users.

Next will choose the create record element to create a contact record. Before that will create a variable in flow to store the created contact ID and this record ID will be used to navigate to the record page using lwc.

 

Whoo!! We are done with our screen flow and it looks like below.

Now we are done with Flow. So let’s create a Lightning wed component to invoke this flow.

To invoke a flow from an LWC, you can use the lightning-flow . The lightning-flow component has the following attributes:

  • flow-api-name: This is the API name of the flow that you want to invoke.
  • flow-input-variables: This is an array of objects that represent the input variables for the flow. Each object must have a name property and a value property.
  • onstatuschange: This is a callback function that is called when the status of the flow changes.

Usage Considerations

The lightning-flow component only supports active flows for the flowApiName attribute.

If you have custom Lightning Web Components or Aura components in your flow, then you won’t be able to use lightning-flow on Experience Cloud sites that use Lightning Web Runtime.

Check what all the changes parameters are available. Let’s create an LWC component.

contactToastMsgLWC.Html

1
2
3
4
<template>
       <lightning-flow flow-api-name='Screen_Flow_Contact' onstatuschange={handleStatusChange} >
       </lightning-flow>
</template>

contactToastMsgLWC.Js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { LightningElement} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { NavigationMixin } from 'lightning/navigation';
 
 
export default class ContactToastMsgLWC extends NavigationMixin(LightningElement){
 
handleStatusChange(event) {
    console.log("event detail",event.detail.status);
 
if(event.detail.status === "FINISHED") {
 
    //Get the flow output variable and store it.
    const outputVariables = event.detail.outputVariables;
             for(let i= 0; i < outputVariables.length; i++) {
                const outputVar = outputVariables[i];
                //contactId is a variable created in flow.
                if(outputVar.name === 'ContactId') {
                    console.log(outputVar.value);
                    if(outputVar.value != null){
                    //Call ShowToast Function
                    this.showToast("Success","Contact Created Sucessfully","success");
                    //Pass the contactId variable value to navigateToRecord.
                    this.navigateToRecord(outputVar.value);
 
                    }else{
                        console.log('contact is not created');
                    }
                    
                }
            }
}
 
if(event.detail.status === "ERROR") {
  this.showToast("error","Error occurred while creation of contact","error");
}
}
//Toast Message
showToast(title,message,variant) {
    const evt = new ShowToastEvent({
        title,
        message,
        variant
    });
    this.dispatchEvent(evt)
}
//Navigate to contact detail page.
navigateToRecord(recordId) {
    this[NavigationMixin.Navigate]({
        type: 'standard__recordPage',
        attributes: {
            recordId,
            actionName: 'view'
        }
    });
}
}

contactToastMsgLWC.Js-meta.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>57.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__FlowScreen</target>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>
 

Outcome

Add the LWC component to any page in Salesforce. Currently, I have added it to the record page.

Tags:
ApexflowJavaScriptLightning Web ComponentlwcSalesforcesalesforce Developer
Prev PostSending Automated Emails to All Related Contact Roles When an Opportunity is Won
Next PostHow to Calculate and Improve Customer Retention Rate
Related Posts
  • Lightning Component(Aura) To Display Contacts Related To Particular Account August 22, 2019

Leave a Comment Cancel Comment

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to get your work history report by PF
  • How Salesforce Delivers Exceptional Service Using Service Cloud
  • How to Calculate and Improve Customer Retention Rate
  • How to Invoke a Flow from a Lightning Web Component
  • Sending Automated Emails to All Related Contact Roles When an Opportunity is Won

Recent Comments

No comments to show.

Popular Posts

March 20, 2024 / Marketing, Salesforce
How to Calculate and Improve Customer Retention Rate

Tags

@isTest @testsetup Admin Administrator Apex Branding Corporate CRR Customers Data Import Wizard Data Loader Data Migration Designing flow JavaScript Lightning Web Component lwc Marketing Mobile Apps Salesforce salesforce developer Test Class test class code coverage unit test Validation Rules Website

Categories

  • Admin
  • Apex
  • Aura Component
  • Development
  • Facts
  • Flows
  • LWC
  • Marketing
  • Mobile Apps
  • Networking
  • Salesforce
  • Service Cloud
  • Social Media
  • Trailhead
  • Web Design

WSM Technosoft’s offering enables clients to quickly augment their Salesforce Consultant teams through product development and support resources located in our development centers. Based upon project’s requirements, WSM Technosoft’s Team can either co-locate at client at site or work from our development center in India.

Our Services

  • Website Designing
  • Salesforce Solutions
  • Salesforce Training
  • Data Migration and Processing
  • Code Review, QA and Support

Recent Articles

  • How to get your work history report

    Aug 21, 2024

  • How Salesforce Delivers Exceptional

    Mar 20, 2024

Useful Links

  • Home
  • Portfolio
  • Contacts
  • Services
  • FAQ
  • About Us
  • Services
  • Blog
  • About Us
  • Contacts
info@websharpmarketing.com
+91 9311 5511 17 Call Us Now
2nd Floor, Devasthali - Plot no. 42/5, Block – A, Sector – 62, Noida, UP - 281203
© WSM Technosoft, 2014–2024. All rights reserved