Launch Salesforce Flow From Apex Class with LWC.

 Hello Trailblazers,

            In this Blog, we are going to learn how to Launch a Flow from an Apex class. I am using Lightning Web Component (LWC) as User Interface.


Basically I have created three input fields and button as well , after clicking 'Submit' button it will fire a Auto Launched flow and that flow will create a Contact record and send an Email with these Three input field data to given Email address.




HTML CODE : 

  • In this component we have Three input fields with onchange event.

<template>
<lightning-card title="Personal Information"
icon-name="action:info">
<lightning-input
name="firstName"
type="text"
placeholder="Enter First Name..."
onchange={handleChange}
>

</lightning-input>
<br/>
<lightning-input
name="lastName"
type="text"
placeholder="Enter Last Name..."
onchange={handleChange}
>
</lightning-input>
<br/>
<lightning-input
name="email"
type="email"
placeholder="Enter Email..."
onchange={handleChange}
>
</lightning-input>
<br/>
<lightning-button
label="Submit"
variant="brand"
onclick={handleClick}
slot="footer"
disabled={checkDisabledStatus}
>
</lightning-button>
</lightning-card>
</template>

 

Javascript File  :

  • Here we are checking input whether it is valid or not and accordingly we are enabling the Submit button. 
  • After hitting Submit button we are sending input data to Apex class method named as "start".

import { LightningElement, track } from 'lwc';
import start from '@salesforce/apex/FlowControllerLwc.start';

export default class FlowFromApex extends LightningElement
{
@track firstName = '';
@track lastName = '';
@track email = '';

_isAllInputsValid = true;
get checkDisabledStatus()
{
if(!this._isAllInputsValid && this.firstName !== '' &&
this.lastName !== '' && this.email !== '')
{
return false;
}
return true;
}

handleChange(e)
{
if(e.target.name === 'firstName')
this.firstName = e.detail.value.trim();
else if(e.target.name === 'lastName')
this.lastName = e.detail.value.trim();
else
this.email = e.detail.value.trim();

this._isAllInputsValid = ![...this.template.querySelectorAll('lightning-input')]
.reduce((validSoFar , inputField) => {
inputField.reportValidity();
return validSoFar && inputField.checkValidity();
}, true) ? true : false;
}
handleClick()
{
const data =
{
firstName : this.firstName ,
lastName : this.lastName ,
email : this.email
};
start({
jsonData : JSON.stringify(data)
}).then(() => {
alert('SUCCESS!!');
this.template.querySelectorAll('lightning-input')[0].value = '';
this.template.querySelectorAll('lightning-input')[1].value = '';
this.template.querySelectorAll('lightning-input')[2].value = '';
}).catch(err => {
alert('Error!!');
});
}
}

 

Apex Class : 

  • In this Apex class we are parsing JSON data which is coming from LWC component and then we are passing these parsed Input data to Auto-Launched flow (by assigning values to Flow variable).

public class FlowControllerLwc
{
public static Flow.Interview.Flow_LWC myFlow;
@AuraEnabled
public static void start(String jsonData)
{
WrapperInfo data = (WrapperInfo)JSON.deserialize(
                                        jsonData, WrapperInfo.class);
Map<String,Object> mapValue = new Map<String,Object>();
List<Object> inList = new List<Object>();
mapValue.put('firstName_Flow',data.firstName);
mapValue.put('lastName_Flow',data.lastName);
mapValue.put('email_Flow',data.email);
myFlow = new Flow.Interview.Flow_LWC(mapValue);
myFlow.start();
}
public class WrapperInfo
{
public String firstName;
public String lastName;
public String email;
}
}

 

Flow Screen Shots : 

Click here or goto https://www.dropbox.com/sh/2v04jncdylqn4cz/AAARXvNsaTtC5cZh9xAgaNqUa?dl=0


Github Link :  
https://github.com/AK-Kool/FlowFromApex
 


Comments

Post a Comment