Flows: How to use Flows for List View Records

In this blog I am going to show how to use and call Salesforce Flows from list views. We will see how can we pass Selected records from a List View into a Flow in order to perform actions on multiple records like Mass Update,Mass Delete or Insert etc.


Calling Flows from List views salesforce



Main Takeaways from this Article:

1.Passing Selected Records from List Views into Flows

2.Using Flows to perform Actions on Multiple Records at once from List Views

Passing Records from List Views to Flows

Flows are quite versatile now in terms of usage and features. I had always wondered if we could use flows in List views just like List View Buttons to perform Actions on Selected Records .And now I know that we can.

There's once catch though. We cannot achieve this without a bit of Visualforce and Apex. But only a little.This will require a very basic Visualforce page and a very simple controller. Even if coding is not your thing don't worry this is very easy to understand and can be easily replicated and used.

The idea is to use create a new List View Button for List Views and use the Visualforce inside that List View Button. This Visualforce will basically help us in getting the selected records from the List view and these records will be passed directly into the flow. Once, we have selected records in the Flow, you can use those records however you want to create any automation. Pretty Cool right!

Scenario : Trigger/Fire Salesforce Flows from List Views

Sample Use Case
Users should be able to select cases from Case List View(any list view depending on requirements) so that they can be updated(Closed). Also, the Subject for selected Cases must also be appended with a text at the end. This appended text should be "Duplicate Closed". 
Basically think of this as a Manual Case Archival where user has the option to close bulk Cases from within the List Views with some automation.

User selects some Cases from a Case List View --> User clicks on a button in List View--> Selected Cases are Closed Automatically--> Case Subject is appended with "Duplicate Closed " 

Please Note: In order to try and showcase a Specific Use Case, I may have overlooked some of the best practises related to Flow Development in this article. Please make sure to follow these in real world scenarios and projects. Check some really important ones below


Solution and Design
As I had mentioned before this will be achieved by using three items.

1. Visualforce Page and Controller: To get the selected Case Records and their field values(whichever fields we require) and pass them to Flow.
2. List View Button: This will be on the List Views and will have be a place holder for Visualforce Page. Clicking this button will call Visualforce page which in turn will call our Flow to perform automations.
3. Screen Flow/Automated Flow: Depends on choice. But I have used a Screen Flow in this example.

1.Visualforce Page & Controller : To Send List of Selected Records (Not just Ids) to the Flow.

We are using Case Object in this example. So we will create a very basic Visualforce page with standard controller of object Case and one extension class which is basically an Apex Controller. The code is simple and can be reused for other objects by simply changing the name with the object of your choice.Checkout code below to understand more.

The Apex Controller of Visualforce will be used to query all the fields of selected Case records which we need to send to the Flow.In our case these fields are Case Id and Case Subject. We will send this list of Case records(not just IDs) into the Flow directly.

This is very important: We are Sending Case Records to Flow. Not Only Case Ids.
Only values of those fields which we query in controller can be directly used/found in the Flow without using GET Element. Basically we are directly sending the List of Case Records with all fields which we need into the Flow and in this way we need not use the GET element to query on these records again to get field values for the records.
Otherwise, if we had only sent Case Ids to the Flow and not the whole case records with field values, we would have had to use GET element in Flows to fetch all the fields from those Case Ids. And since, there can be multiple case records selected, GET element would have been used in a Loop which is always an avoidable thing as it can hit Governor Limits.

We can directly call any Flow from Visualforce and pass values into its variable(the flow variable should be able to accept input values and should be of type Record).We will see this later in the article.

Important:
The Flow name and input variable should be same which we provide in the Visualforce Page.
Checkout the code. And Remember, just change the object name from the object of your choice instead of Case to use this code for any other object too. It's simple and straightforward. 

Here is the Apex Controller Code. Again, please remember to query all the fields to be required/sent to the Flow. 

ListViewController.cls
Here is the VisualForce Page Code. Remember, we need to have the Flow with the name given in the parameter interview name and also that flow should have a matching variable as we use in the page.
Important Note: Create a blank/empty Flow and also create a Variable of type Record in Flow before creating Page and Controller.Save and Activate the Flow.We need to use Flow name and Input variable name in Visualforce so they should exist before creating this page. I have shown how to create this input variable later in this article. Please check that out before creating this page and controller.

CaseListView.vf

As per the VF and controller code, in our case, the Flow name is "CaseListView" and the input variable in the Flow that will be accepting the Selected Case Records from Visualforce is named as  "CasesSelectedRecs". We will see how to create this variable later in the article. 
Also, this variable is assigned the list of Selected case records from the Controller through the variable named "LstSelectedCases'. Checkout the code again for more clarity.

2. List View Button
Now since we are done with VF and controller, we need to create a new button on Case Object of type List View. We will use our Visualforce page as its content source.
I have named the button as "Archive Cases".
Use flows in List Views

3. Flow Design

A Flow will be created before everything for this requirement.Flow name is "CaseListView". 

Important: I am creating a Screen Type Flow. But an autolaunched Flow can also be used to send data from visualforce directly. It's up to you as per requirement. 
Using Salesforce Flows in List View
Now let's jump into action and create this Flow. Flow has 5 elements and Two main Variables.

1. Input Variable : To Accept the List of Selected Cases from Visualforce.
First of all, we will create a New Input variable of type,Record and Datatype Case.
This variable can hold multiple records. Also, this variable will me marked as available for input so that it can receive values passed to it from outside of the Flow.

In our case, values to this variable(CasesSelectedRecs) will be passed from the Visualforce page as seen above in the Visualforce Code.


2. A Variable of type Record(Multiple Values) to hold the Cases which have to be Updated. 
This variable is similar to variable above. But it is used to update Case records. This is also of type Case and can also hold multiple values. How the values are assigned in this will be seen later in the flow.

3. Decision Element to Check if any Cases were Selected or not.
To start with flow, we will first create a decision element to check if user selected any cases from list view before clicking the button or not.This we can do by checking if the input variable which receives values from Visualforce (CasesSelectedRecs) is Null or not.Only if is not null, process will process further.

4. Loop Element to Loop on these Cases.
If user had selected any cases, we will use a loop element to iterate over the selected case records. We will loop over the input variable "CasesSelectedRecs" which received selected cases from visual force Page. 

After Summer'20 release, we don’t  need to create a loop iteration variable. It is automatically created.

Checkout Video of this along Assignment after step 5 below.

5. Assignment Element to Assign new Status and Subject values as per the requirement.

We will now loop over selected Case records one by one and assign the Case Status for every looped Case as Closed. Also, we will add a string value "Duplicate Closed" at the end of the each Cases's Subject as per the requirement. 
Remember, if we didn't query Subject in our SOQL query earlier in Apex Class, we wouldn't have been able to use it here.

Checkout Video for Steps 4 and 5:


6. Assignment Element to add the Individual case record from the Loop into the Cases to Update Variable from Step 2.
This is used to add the current case record being looped into the newly created variable to store all the updated case values. So, basically, in every iteration of the loop, we are adding the updated case record to this new record variable which will be used to update cases later.

7. Update Element to just update the Cases to Update variable(step 2) having the updated values.
After the loop ends, We will use the update element to update the records.

We will just update the case update list variable which created to add Cases from loop.

We are done. 

Add the List View Button on the Case List View. This can be done by using Search Layout. https://trailblazers.salesforce.com/answers?id=90630000000grS1AAI

Checkout below demo video and see the Flow in Action.




If you like this please Share! #Learn and Share!

Enter your email address:


Delivered by FeedBurner

Checkout my other posts here:

Comments

Post a comment

Thanks for your Feedback!


Never Miss Latest Posts