How to Delete Related Records Using Flow Action in Salesforce

In Salesforce, we can automate the process of deleting the records whenever we want using the Flow Builder.

In this Salesforce tutorial, we will learn how to delete related records using flow action in Salesforce with the help of real-time scenarios and step-by-step explanations.

Let’s take a Scenario while working as a Salesforce Admin for a US-based Organization that provides software solutions; I was assigned the task of whenever users clicked the delete button from the account object, all cases should delete which status is “Closed.”

To create this process automatically, we can create a Screen flow flow. In which we will create a button on the Account object, and when the user clicks on it, the screen will pop up and confirm whether you want to delete records or not. If the user clicks on the yes button, then all cases will delete those in the Closed status.

How to Delete Records Using Salesforce Flow

Using the above scenario and the following steps, we can delete records using Salesforce Flow.

Create Screen Flow:

1. Log in to Salesforce Org. -> Click on the Setup -> Home Tab -> In Quick Find Box, Search for Flows. You will see an option under Process Automation -> Click on Flows.

Then, click on the New Flow button to create a new flow.

2. To display messages and receive user input, we need to create a Screen Flow. In the new flow window, select the Screen Flow option and click on the Create button.

Now, we will create the following elements in the flow according to the flow diagram displayed in the following Flow Diagram.

Deletion of related records using Salesforce Flow

1. Get Records (All Related Cases):

1. First, we have to create a New Resource and store the Account ID in it. To do that, we need to create a new variable. To do that, click on the Manager tab, and then you will see the New Resource option. Click on it.

Delete the Related Records Using Salesforce Flow

2. Then, in the Resource Type, select Variable Type. Enter the API name the same as recordId; do not change it. Select Data Type as Text and click the checkbox for Available for Input.

See also  Create Record Trigger Flow in Salesforce | What is Record Trigger Flow in Salesforce

Then click on the Done button.

Delete Related Records Using Salesforce Flow

3. Then, we need to Fetch all Case records related to the Account object. To do that, click on the Add Element icon and then select the Get Records element under the Data component.

When you click on get records, you need to select an object from which you want to fetch the records. Here, I have selected the Case object. Then, in the Filter Case Records, store AccountID in the RecordID available, which we have created in New Resources, and Status is equal to Closed.

Delete a Related Records Using Salesforce Flow

2. Decision (Check Null Records):

4. Here, We need a Decision element to check whether the records we are fetching are Null or Have values. When you click on the Decision element from the add element, you need to enter the following necessary details.

Here, we need to enter the element Label, which displays on Flow Canvas. The API Name will automatically populate. It will be used to back functionality.

5. Then, we need to define two Outcomes: the first is Not Null, and the other is Null.

  • Not Null: In the not null outcome, we need to add a condition that the records we got from the case should not be null. The condition will be Get Records(API Name) – Is Null – False.
  • Null: There is no need to add any condition here; only change the Label of the Default Outcome to Null.
Deletion of a related records using Salesforce Flow

3. Screen (Display Null Msg)

6. Then, if any record contains Null values, we will show a message to the user. For that, we need to add a Screen Component to the flow. You can see I have added a scene element and entered a message to display if there are any Null records.

How to Deletion of related records using Salesforce Flow

4. Screen (Confirmation):

7. Here, we will add a screen to collect input from the user to confirm whether he wants to delete records. For that, we need to add a Text field and Radio buttons.

See also  Automatically Add Users to Public Group Using Salesforce Flow

I have given radio buttons labeled as Yes and No and also the same API Name because the API name we will need to give reference to the Delete record.

Deleting Records Using Salesforce Flow
How to Delete Related Records Using Salesforce Flow

5. Decision (Delete or Not):

8. As in the Confirmation screen, we have two choices: Yes and No. In this Decision element, we will set what will happen if the user selects the Yes or No option. For that, add a decision element and enter the Label and API Name.

Then, we need to define two Outcomes: the first is Delete Cases, and the other is Do Not Delete.

  • Delete Cases: In the delete case outcome, we need to add a condition that the on which radio button we want to add Delete Record. The condition will be Confirmation Screen (API Name) – is equal – Yes (Radio Button API Name).
  • Do Not Delete Cases: You do not need to add any condition here; only change the Label of the Default Outcome to Do Not Delete Cases.
How to Delete a Related Records Using Salesforce Flow

6. Delete Records (Delete Related Cases):

9. When the user clicks on the Yes radio button, the flow in the backend will redirect to the Delete Records element, where the records are deleted. To add the Delete Record element, click on + Add Element and add the element.

Then, enter the Label and the API Name. The description is optional. You can give a short intro about this element.

10. Now, here is an option for How to Find Records to Delete so we have the Get Records element where we have stored all records. So here, we will select Use the IDs stored in a record variable or record collection variable. Then, you will see the option to Record or Record Collection. Here, you need to select Get Record API name and Done.

How to Delete the Related Records Using Salesforce Flow

7. Screen (Deleted Msg):

11. Now, we will add a screen component to display the message to the user that the records have been deleted Successfully.

See also  How to Freeze User's Account Using Salesforce Flow

8. Roll Back Records:

12. The rollback component is used if any error occurs during the deletion of the records that are in the transaction. The records will not get deleted, and the transaction will exit. To add it, click on the Delete Record element, click on the Fault Path, and then add Rollback records.

Enter the Label and API Name, and then click on the Done button.

Delete a Related Records Using Salesforce Flows

10. Save Flow:

13. Then, click the Save button to save the flow. For that, we need to enter the Flow Label; here, I have entered Delete-related cases using the Salesforce Flow. The API Name will automatically populate as we click the text box. The Description is optional; we can introduce our flow Shortly.

deleting related records using Salesforce Flow

Now we have to add Screen flow on the Account object with the Quick Action. For that, you can read the whole article on the Call a Flow From Button in Salesforce.

This is how we can delete related records using Salesforce Flow.

Conclusion

I hope you got an idea about how to delete related records using Salesforce flow. In this article, we have seen how to delete related records using Salesforce flow with real-time scenarios and step-by-step explanations.

You may like to read: