Tips & Tricks

Quick Trick: Add a Delete Button to Any Related List in Salesforce

Ever wanted to add a delete button to your related lists and allow users to wipe out those pesky records on a list view? Well you can - pretty easily in fact!

First off, huge shoutout to Ram for this super helpful blog post. You're a lifesaver, Ram! 🙌

Create your button with the steps below:

Delete Selected Records Animation

Step 1: Create the Flow via Ram's Blog Post

Start by following the detailed guide in Ram's blog post, How to delete multiple records using List View Button. This will create a basic flow that enables record deletion from list views. Go ahead and follow all the steps:

  1. Create the Flow
  2. Create the list view button
  3. Add the button to your page layout

Test it the flow and try to delete the records. Pretty cool huh?

While this approach works well for a single object, it has limitations when scaling across multiple objects - each requiring its own separate flow. For organizations needing bulk deletion capabilities across multiple objects, a more efficient solution is needed. That's where our enhanced approach comes in.

Step 2: Create an @Invocable Apex Classes

Here's where it gets really cool. We're going to use an Apex class that can handle any IDs you throw at it. Check out this AgnosticDeleteInvocable class:


public class AgnosticDeleteInvocable {
    @InvocableMethod(label='Delete Records Agnostically' description='Deletes records of any object type based on provided IDs')
    public static void deleteRecords(List requests) {
        List recordsToDelete = new List();
        
        for (DeleteRequest req : requests) {
            String objectApiName = req.objectApiName;
            List recordIds = req.recordIds;
            
            for (Id recId : recordIds) {
                SObject obj = recId.getSObjectType().newSObject(recId);
                recordsToDelete.add(obj);
            }
        }
        
        if (!recordsToDelete.isEmpty()) {
            delete recordsToDelete;
        }
    }
    
    public class DeleteRequest {
        @InvocableVariable(required=true label='Object API Name')
        public String objectApiName;
        
        @InvocableVariable(required=true label='Record IDs')
        public List recordIds;
    }
}
            

Step 3: Clone the original Flow and give it an appropriate agnostic name.

Flow Name: Bulk Delete sObject records via Id(s)

Remove the Delete Element and replace it with the 'Delete Reocrds Agnostically' Apex action

Populate the Object's API Name and the Record Ids variable.

Agnostic Flow