How to invoke lightning Auto launched Flow from the Apex class

By | December 6, 2021

In this blog, we will discuss on how to invoke a lightning Auto launched flow from the apex class.

The flow in Salesforce can be launched without user Interaction such as from a process builder or the apex code. So here we are creating a auto launched flow and call it through the apex code.

Let’s take a simple example to understand how to call a lightning flow from the apex.

Example

In this example, we are getting the value of Opportunity Stage name based on the opportunity id that we are passing from the apex code.

Firstly, we have created lightning auto launched flow in which we have created two variables first is recordId and it is set as ‘available for Input’ and second is Opportunitystage which is used to send output to the apex class, and it is set as ‘available for Output’. Kindly refer below image.

RecordId Variable
RecordId Variable
Opportunity Stage Variable
Opportunity Stage Variable

Secondly, to get opportunity records we have added ‘Get Records’ element in flow. Further more, we assign opportunity stageName field to the ‘Opportunitystage’ variable as shown in below image.

Get Opportunity Record
Get Opportunity Record
Get Opportunity Record Filter
Get Opportunity Record Filter

For instance, to call flow from apex class, create an instance of an Interview object by using ‘Flow.Interview.flowName’. It uses start() method to execute your flow.

Lightning Flow
Lightning Flow

For example, refering to the apex class code below, you will notice we have created the instance of Interview object and passes opportunity Id as a parameter.

//Create parameter
Map<String, Object> Params = new Map<String, Object>();
Params.put('recordId','006p000000Aal2EAAR');

//create instance of Interview
Flow.Interview.GetOpprtunityStageName stage = new Flow.Interview.GetOpprtunityStageName(Params);

//Invoke start method
stage.start();

//get value from Flow
string stageName=(string)stage.getvariableValue('opprtunityStage');
system.debug('stageName::: '+stageName);

As a result, after execution of flow it will return the stage of that particular opportunity which id we have passed as a parameter. kindly refer to the below image.

Debug Result
Debug Result

In conclusion, using the steps above you can call lightning auto launched flow from the apex code.

We hope that you find this blog helpful, if you still have queries, don’t hesitate to contact us at salesforce@greytrix.com.

About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts