How to use Lightning Flow as List View in Salesforce?

2
2004

Hello #Trailblazer,

Welcome back, In this blog post we will see how to use a lightning flow as a list view button inside salesforce for any object.

Use Case

Create a Flow which will act as List View Button on Lead Object and when a user selects multiple records and clicks on our custom button “Convert Lead” then it should convert all the Selected Lead Records and Navigate back to Lead Object List View.

Implementations

As we know that we can not use flow directly in a ListView button, we will use an intermediate flow which will include the flow inside the VF page.

So in order to implement the solution, we will follow the below steps

  1. Create an Apex Class which will have an Invocable method to convert the Lead
  2. Create an Screen Flow which will call the Invocable Apex and pass the required parameters inside the invocable apex
  3. Create another Apex Class which will act as an Extensions of the VF page which will give us the record ids of the selected records and also the selected records with the fields which we want to display over screen.
  4. Create the VisualForce Page which will have the Flow inside it and also will pass the required parameters to Salesforce Lightning Flow
  5. Create the List View button and add it to Lead Search Layout
  6. Test the button

Step1 – Create the Apex Class with Invocable Method

The very first step is to create an apex class which have an Invocable method inside it to convert the lead. Below is the code which we will be using for the demo.

Public class AutoConvertLeads {
@InvocableMethod(label='Custom Lead Convert'
description='Apex Class converts the leads based in the Ids sent from flow'
category='Lead')
public static List<List<String>> LeadCovert(List<List<String>> leadIds){
// Query on the LeadStatus Object to find the correct converted status
LeadStatus convertedLeadStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();
List<String> leadIdsToConvert = leadIds.get(0);
// Loop with the Records Ids and prepare to Conver leads
for(String currentlead: leadIdsToConvert){
Database.LeadConvert leadConvert = new Database.LeadConvert();
leadConvert.setLeadId(currentlead);
leadConvert.setConvertedStatus(convertedLeadStatus.MasterLabel);
leadConvert.setDoNotCreateOpportunity(FALSE); //Remove this line if you want to create an opportunity from Lead Conversion
leadsToConvert.add(leadConvert);
}
if (!leadsToConvert.isEmpty()){
List<Database.LeadConvertResult> leadConvertResult = Database.convertLead(leadsToConvert);
}
return leadIds;
}
}

Step2 – Create the Screen Flow

Now, as we have created the helper class with an Invocable Apex Method, time is to create the Flow

  1. Login to Salesforce
  2. Go to Setup
  3. In the Quick Find box search for Flows
  4. Select Flows
  5. Click on New Flow button
  6. Once the Screen Pop-up Select Screen Flows
  1. Click on + icon
  2. Select Screen Component
  3. Provide the Label and API Name will be auto populated
  4. Add the Display Text Component to the Screen with the given message “Click Next to convert the selected lead records.”
  1. Click on “New Resource” From the left side bar
  2. For the resource type select variable
  3. For the API Name give “varLeadRecordIds”
  4. For the Data Type select “Text”
  5. Check “Allow multiple values (collection)” checkbox
  6. Check “Available for input” checkbox
  1. Click on “New Resource” From the left side bar
  2. For the resource type select variable
  3. For the API Name give “vaLeadRecords”
  4. For the Data Type select “Record”
  5. For Object select Lead
  6. Check “Allow multiple values (collection)” checkbox
  7. Check “Available for input” checkbox
  1. Click on + Icon again
  2. Select Action from the available icons
  3. Search for “Custom Lead Convert” and select that action

Save and Activate the flow

Step3 – Create VF Page Extension Class

This Apex Class will be responsible for getting all the selected lead records and then preparing the public variables which we will be passing to the flow as an input. “VE_AutoLeadController” use this as the name of your Apex Class and get the code from below Gist.

public class VE_AutoLeadController {
public List<String> leadIdsSet { get; set; }
public List<Lead> selectedLeadRecords { get; set; }
public VE_AutoLeadController(ApexPages.StandardSetController setController) {
selectedLeadRecords = (Lead[])setController.getSelected();
leadIdsSet = new List<String>();
for(sObject sobj : setController.getSelected()){
leadIdsSet.add( (String)sObj.get('Id') );
}
// SOQL on Lead Object if you need more Fields
selectedLeadRecords = [SELECT Id, Name, Company, Phone, Email, Rating FROM Lead WHERE Id IN: leadIdsSet];
}
}

Step4 – Create VF Page

The VF Page will use “VE_AutoLeadController” Apex class as an Extension and “Lead” as standard object because we will use this VF page for Lead Object List View. Page will also have the Flow inside itself

To use the flow inside VF page we use a special tag which is “flow:interview“, this tag requires min 1 variable which is the API Name of our Salesforce Flow. We can also pass the variables from VF to flow. Learn more about “flow:interview” tag. Use below code for the same

<apex:page standardController="Lead" recordSetVar="leadRecord" extensions="VE_AutoLeadController" lightningStylesheets="true" >
<apex:slds />
<!-- LEFT({!$Api.Partner_Server_URL_260}, FIND( '/services', {!$Api.Partner_Server_URL_260})) -->
<!-- LEFT( {!$Flow.FaultMessage}, Find("You can look up ExceptionCode", {!$Flow.FaultMessage} ) -1) -->
<apex:pageBlock title="Selected Records">
<apex:pageBlockTable value="{!selectedLeadRecords}" var="lead">
<apex:column headerValue="Name" value="{!lead.Name}" />
<apex:column headerValue="Email" value="{!lead.Email}" />
<apex:column headerValue="Company" value="{!lead.Company}" />
<apex:column headerValue="Rating" value="{!lead.Rating}" />
<apex:column headerValue="Phone" value="{!lead.Phone}" />
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Convert Leads">
<flow:interview name="List_View_Flow"
buttonStyle="slds-button slds-button_brand"
rendered="true"
buttonLocation="bottom"
finishLocation="{!URLFOR($Action.Lead.Tab, $ObjectType.Lead)}"
showHelp="true" >
<apex:param name="varLeadRecordIds" value="{!leadIdsSet}"/>
<apex:param name="varLeadRecords" value="{!selectedLeadRecords}"/>
</flow:interview>
</apex:pageBlock>
</apex:page>

Step5 – Create List View Button

Now, as we have developed all the components, the time is to create a list view button and add it to the list view. To Create the Button follow the below instructions.

  1. From the Setup –> Go to Object Manager
  2. Search for Leads
  3. Click on “Button Links, and Actions
  4. Select “New Button or Link
  5. Create the Button based on the Below Configuration

Step6 – Add the button to Search Layout

Follow the below steps to add the button to page layout

  1. From the Same Lead Object Page, Click on the “Search Layouts for Salesforce Classic
  2. Click dropdown next to “List View” and then Click on Edit
  3. Add your buttons from the “Available Buttons” to “Selected Buttons” Section
  4. Save the Changes

Step7 – Test the buttons

From the Lead Tab, Select “All Open Leads“, Select Multiple Records and then Click on your Custom Button

Final Video

Thanks for reading 🙂 Happy #Trailblazing

2 COMMENTS

  1. Hi Amit,

    Is it possible to skip the VF page here? Actually I don’t want to redirect page from the ListView. I just want to add one button at ListView and on click “Lead Convert” then display show message (Success/Error).

    Regards,
    Puneet

LEAVE A REPLY

Please enter your comment!
Please enter your name here