In this article, we will provide a detailed step-by-step guide on record deletion using Salesforce Flow.
Delete the related opportunity records on Account object if the Stage of the Opportunity is equal to closed lost.
Creating the Flow
We will create a Screen Flow and place the Flow on the Account object records quick actions.
To begin the record deletion using Salesforce Flow, we’ll first obtain the Account record Id. This Id will help us identify and examine the related Opportunity records associated with the Account.
We will create a Flow resource of variable type to store the Account record Id.
In Salesforce Flow, a resource variable enables you to store and reference a resource, such as a custom formula or recordId in your Flow.
The data type for this variable will be text. Since we are going to provide the Account record’s Id as input in our Screen Flow, we need to set Available for Input checkbox to True.
Want to Learn Salesforce Flows? Checkout our Salesforce Flow Course
Now we will fetch all the related opportunities in the Closed Lost stage using the Get record element.
As we are fetching all the related closed lost opportunities of an Account, under the How Many Records to Store criteria we will select All records.
By utilizing the “Add Fault Path” feature on the Get Records element, you can set up an alternative path to capture and handle errors that may occur during the process of fetching opportunity records from the Salesforce org.
Now incorporate the Decision element into the Flow to verify the presence of opportunity records in our Get Records element.
If it contains some records, we will display the user whether we want to Delete the records or not.
If no opportunity records are found, a message will be displayed to the user indicating, “This Account record does not have any related opportunities in the closed lost stage.”
To display this message, we will add a screen element to the Null path of the decision element.
We will also update the footer section of the screen element by hiding the Previous and Pause buttons.
Upon fetching an opportunity with the “closed lost” stage, users will be presented with the option to either delete the opportunity records or retain them.
To offer these choices to the user, we’ll incorporate the screen element and utilize the radio buttons component.
Optionally, you can set default values to be pre-selected for these radio buttons.
After providing the user with the option to choose whether to delete the opportunity or not, we will use the decision element to check their selected option.
Delete Element in Salesforce Flow
To delete the opportunity record, we will use the Delete Records element. We have two options to find the records to delete.
- Use the IDs stored in a record variable or record collection formula
- Specify conditions
Having the Get record element storing all related opportunities of our Flow, we will proceed with the first option.
If an error occurs while deleting the records, we can roll back the transaction using the fault path, preventing the deletion of the records.
Add another screen element below the delete records element to show the user the message “All related opportunities with closed lost stage have been deleted.”
Now our Flow is complete. Save the Flow and Activate it.
Add Flow to Account Object Quick Actions
Object Manager ➡ Account Object ➡ Button, Links and Actions
Create a new action by clicking on New Action button.
Navigate to the Account page layout and add this newly created quick action on the Account Layout.
Open any Account record and you will be able to see the quick action button been added to the page layout.
Verify whether the Account record contains any opportunity with the stage equal to closed lost. If not, following message will display on the screen clicking the Delete Lost Opps button.
If the Account record contains opportunity with the stage equal to closed lost and the user selects to delete the opportunity record then following message will display on screen.
Business use case where we can perform record deletion using Salesforce Flow
- Automate removal of completed or outdated events/tasks created by sales representatives, ensuring a clutter-free view of upcoming tasks.
- Automate a check for duplicate accounts or contacts created during the lead conversion process, and remove them when converting a lead into an opportunity, which creates an account and a contact.
- Remove the contact from specific email list if the contact unsubscribes from receiving any email communications from your company.
- Automate removal of duplicate accounts from the Salesforce database based on specific criteria, like no recent activity.