Create a Mass Delete List View Button in Lightning (without code!)

In the Classic Salesforce user interface, creating a mass delete List View button was quite easy. The old tried and true method involved using Java-script within an execute Java-script / on-click Java-script button, to handle parsing the multiple record id’s (from the selected items in the List View) and performing the deletion. Unfortunately, Java-script buttons are no longer supported in Lightning Experience, and so do not appear as available List View buttons in the Search Layout configuration.

Many people have tried to work around this, usually by using either Apex code or custom Lightning Component. This got me to thinking, can this be done with nothing more than simple configuration using Visual Flow? The short answer; yes, of course! Let’s take a look at how we can accomplish this.

First, you will need to build the Flow to perform the actual deletion of records when our List View button is used.

Not everyone is aware (and official documentation appears to be quite limited on this) but Visual Flow supports a few “reserved” variables that will become automatically populated, depending on the context that the Flow is run from. For example, if you create an input text variable called “recordid” in Flow, and this Flow is called from a Quick Action on a particular object, this variable will be automatically populated with a record id when the action button is clicked from a record page. It just so happens there is also another reserved variable named “ids” that when set as an input text collection variable will populate with multiple record id’s passed into the Flow. Ah Hah!

Create a new Flow via Setup > Process Automation > Flows. For this example, I will be creating a Screen Flow to prompt users before performing the deletion and let them know once the deletion is complete. I am also basing this on the Account object, so all record variable’s will need to reference Account and the List View button we create at the end will be on Account.

The next step is to create our variables for use within the Flow. Here’s a quick list of all the required variables and their data type.

  • ids (text collection, input enabled)
  • varId (text)
  • recAccount (record)
  • lstAccountsForDelete (record collection)

The collection variable that will be populated with the selected record id’s from the List View is called “ids”. When creating this, ensure the name for the variable is “ids” and that this is of data type “text” with the “Allow multiple values (collection)” option selected. Also ensure the “Available for input” option is enabled. If this isn’t enabled, the Flow will have an error when run from a List View button.

Next we create a simple Screen element with a Long Text Area component to display the list of id’s and a confirmation message.

As the “Delete Records” Flow element that we’ll need to use to perform the deletion only supports deletion via conditional filtering or by referencing a record variable or record collection variable, we will need to convert our text collection variable into a record collection variable. For this we add a “Loop” Flow element to loop through the “ids” text collection variable and return each id in a single text variable (varId in this example).

Next we need an “Assignment” element to assign the current value of our loop variable “varId” to the id attribute of the record variable “recAccount”.

Next we add another “Assignment” element to add the record variable to our record collection variable, for later use by the “Delete Records” element.

Now we’re finally ready for the actual deletion. Add a “Delete Records” element and with the option “Use the IDs stored in a record variable or record collection variable” enabled, specify our record collection variable “lstAccountsForDelete”.

Finally I add in another “Screen” element with a message confirming the records have been deleted. The final Flow will look something like this in Flow Builder:

After saving and activating the Flow, copy the URL for the Flow from the Flow details for use in our List View button.

That’s the bulk of the configuration done! Now we just need to create our List View button and add this to the Search Layout configuration.

Go to Setup > Object Manager > Account > Buttons, Links & Actions and click “New Button or Link”. Ensure the Display Type for this button is “List Button” with the “Display Checkboxes (for Multi-Record Selection)” option selected and Content Source set to “URL”. Paste in the URL we copied from the Flow details page. In order to prevent the Flow re-running after completion, we need to supply a return URL. This is done by adding the “retURL” parameter to the URL we pasted in. In this case I’m telling this to return to the main Account page ( 001/o). Here is an example of the URL I have.

/flow/Mass_Delete_Accounts?retURL=001/o

At this stage the return URL used to redirect Flow to another page upon completion doesn’t appear to work correctly in Lightning Experience. However, specifying this does at least prevent the Flow from re-running, which can lead to errors – especially for a deletion Flow! More information about setting Flow finish behaviour can be found here: https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_url_set_retURL.htm

Now add the new button to the List View via the Search Layout configuration under Setup > Object Manager > Account > Search Layouts. Use the “Edit” option from the drop-down menu next to “List View”.

Add the new List View button and click Save.

Now when we visit any Account List Views (except Recently Viewed) our new button will be available for use. We can now select multiple Accounts and click Mass Delete to delete all of the selected Account records in one foul swoop! This Flow setup can be modified to work with other multi-select scenarios for updating multiple records at once. Pretty darn cool, and all without any code!