Hello friends, In this post we will learn, how to invoke a lightning auto launched flow from apex class. In salesforce, we can launch the flow without user interaction such as from a process builder or by using apex code. so here we will go through the apex code example. let's get started.
Call lightning auto launched flow using apex
Use Case : In this example, we are getting the list of contact related to an account id that we will passed through apex code.
To understand this example, first we need to create a lightning auto launched flow. so please follow the below steps:
Step 1. Navigate to Setup | Process Automation | Flows. Click on New Flow button.
Step 2. Select Autolaunched Flow(No Trigger) template and click on Create button.
Step 3. Create a variable of Text type and set it as 'available for input'. This variable will be use to pass account recordid.
Step 4. Create second variable of Record type and set it as 'available for output' as well as 'Allow multiple value (collections)'. This variable will be used to get contact list in apex class.
Step 5. To get contact list, we have to add 'Get Records' element in our flow. so go ahead and add get record element with the following configuration shown in the below image.
Step 6. Your flow should be look like the below image. Hit the 'Save' button and then click on 'Activate' button to make active the flow (In case of apex code, it's not necessary to activate the flow, you can just save the flow and start using it in the apex code).
Code Example: Now we can use the above created auto launched flow in the apex code, please refer the below code sample.
Apex Code:
//Create parameter(Please replace below record id with your salesforce org account id) Map<String, String> params = new Map<String, String>{'accountId'=>'0016F00002x1j7m'}; //Create instance of flow interview Flow.Interview.GetContactRecords objflow = new Flow.Interview.GetContactRecords(params); //Invoke start method objflow.start(); //Get value from Flow List<Contact> lstContact=(List<Contact>)objflow.getvariableValue('lstContact'); for(Contact con: lstContact){ System.debug('Contact::: '+con); }Output:
0 Comments
Post a Comment