How To

Delete Salesforce Records Associated via a Lookup Relationship Using Clicks, Not Code


There may be situations where you need to delete related records when you delete the main record. In a master-detail relationship, this is done for you. Deleting the master record, deletes all detail records. However, in a lookup relationship, if you delete the lookup record, the associated records remain. This would result in orphaned records, causing data issues.

Here are a few lessons learned from implementing this use case:

  • Learn how to delete records using flow.
  • Learn how to invoke a flow from a process.
  • Provide descriptions, where provided, in Salesforce. This may be tedious step, I know, but your future self will thank you when you are trying to remember what you configured or assist other/future admins when troubleshooting or enhancing what was built. This includes variables, the purpose of a flow, what each flow element does, etc.

Business Use Case:  Addison Dogster is the system administrator at Universal Containers.  Mary Markle is the Director of Sales. She would like to delete contacts who are updated to an inactive status (meaning the contact no longer does business with the company). When contacts were deleted that had lookup relationships to other records, those records become orphaned, causing data issues, or are not able to be deleted, when there are associated cases, until those records are deleted.


View image full screen

In this situation, Addison Dogster, the system administrator, is able to create a process to execute when the contact’s status is updated to inactive, which will fire a flow to delete the cases (if there are any) before deleting the contact (to be completed by Addison).



Quick Steps:

1. We need to create a custom picklist field Status on the Contact object that is used as a processing field for our process. Go to (Classic) Setup | Customize | Contact | Fields or (Lightning Experience) Setup | Object Manager | Contact | Fields & Relationships, create a new field. Added “Active” and “Inactive” as picklist items with “Active” as the default.

Best practice tip: Provide a description so you and other/future admins know what this custom picklist field is used for.


Set the FLS (visible and edit) to those profiles who can change the status field.

2. Now, let’s create the flow. In Lightning Experience, it is found under Process Automation | Flows. In Classic, it is found under Create | Workflow & Approvals |Flows.

Here is the flow we are going to create. This flow will lookup the Case object where the contactId matches that which will be passed from process builder (varContactID) and store the records in a collection record. Then there is a decision to determine whether the collection has at least one record and if so, delete the records in the collection.

Note: If your contact has other lookup relationships, you would continue the flow by repeating these three steps for the other object relationships.


A. Let’s create our flow resources.

The first variable resource is called “varContactID” which stores the ID passed from process builder. This has a data type of Text.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.


The variable “sCollectionofCases” will hold the case record(s) we find in our query in our first flow step Get Records. This is a sobject collection variable as store one or more Salesforce records by checking “Allow multiple values (collection).” This is denoted by specifying the Data Type as “Record.”

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.


B. Next, we will add a Get Records flow element on the case object where the Id equals the varContactID. We will store the record(s) found in the Record Collection Variable “sCollectionofCases.” Make sure you check the box “When no records are found, set specified variables to null.” We will use this in the Decision flow element in our next step.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.


C. We are going to determine whether our query returned at least one record by using the Decision flow element. The Cases Found outcome’s criteria is {!sCollectionofCases} Is Null {!GlobalConstant.False}. The default outcome is “No Cases.”

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.


D. We will use a Delete Records flow element. We will delete all the records in the collection variable “sCollectionofCases.”

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.


E. Connect the flow elements and outcome connector to match the below…


F. Save/Save As and provide the following properties. Ensure the type is “Autolaunched Flow.”

Best practice tip: Provide a description so you and other/future admins know what this flow is used for, include where it is invoked from.


G. Click the “Back” button.

H. Locate the flow and click on the “Delete Cases Associated to an Inactive Contact” flow name.

I. On the flows screen, activate the flow.


3. Now, we need to create a Process (Classic – Create | Workflows & Approvals | Process Builder or Lightning Experience – Process Automation | Process Builder)to invoke the flow when a contact’s status is updated to “Inactive.”


A. Create a new process.

  • Process Name: Take Action on a New/Existing Contact (This follows the best practice of creating one process per object)
  • Description: Process that runs on a new or existing contact
  • The process starts when*: When a record changes
  • That’s it! You’ve configured the changes.

B. Specify the object: Contact, when a record is created or edited.


C. Specify the criteria node. We want this to execute when the status field is changed and the status is changed to “Inactive.”

  • Criteria for Executing Actions: Conditions are met
  • Set Conditions
    • [Contact].Status__c Is changed Boolean True
    • [Contact].Status__c Equals Picklist Inactive
  • Conditions: All of the conditions are met (AND)


D. Set the immediate action to invoke the flow “Delete Cases Associated to an Inactive Contact” where we set the flow variable varContactID Field Reference [Contact].Id.


E. Activate the process.

Now, before you deploy the changes to Production, don’t forget to test your configuration changes.

  1. On the contact record, create a few case records.
  2. Update the status from “Active” to “Inactive” and save.
  3. Verify that the cases are deleted.

Deployment Notes/Tips:

  • The flow, process and custom field can be deployed to Production in a change set (or can be deployed using a tool such as Dreamfactory’s Snapshot).
  • Don’t forget to update the custom field’s FLS for the profiles that will use the field.
  • You will find the flow in a change set under the Flow Definition component type.
  • Activate the flow and process post deployment as they deploy inactive in Production, unless with Winter ’19, you have opted in on the Process Automation Settings screen, to “Deploy processes and flows as active.” NOTE: With this change, in order to successfully deploy a process or flow, your org’s Apex tests must launch at least 75% of the total number of active processes and active autolaunched flows in your org.