In Winter 21, Salesforce introduces the Before Delete record-triggered flow. This allows you to automate changes to related records or actions prior to deleting the record.
A few things to know about Before Delete flows:
- You cannot use a before delete flow to prevent a record from being deleted like you can with a Before Delete trigger.
- You cannot call subflows out of the box. However, until Salesforce provides that ability in Flow Builder, you can install an apex action Call Subflow created by Narender Singh.
- You cannot use the debug feature with record-triggered flows. Like processes, you need to activate the flow and test it in the sandbox.
Here are a few lessons learned from implementing this use case:
- Learn how to create a before delete flow.
- 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 the account or contact owner know when an opportunity in closed lost stage is deleted by updating the description field of the associated account or contact record and sending an email to the associated account or contact owner.
Solution: Before the closed lost opportunity record is deleted, we want to update the associated account or contact record and send an email to the account or contact owner of the opportunity. Using clicks instead of code, Addison is going to use the Before Delete Record-Triggered flow to handle the task.
The outcome looks like this:
This flow does the following: starts when a opportunity record is deleted where the stage is Closed Lost. (1) It determines whether the opportunity is associated with an account or contact, (2a) if it is associated with an account, update the description field of the account record and (3a) send an email to the account owner or (2b) if the opportunity is associated to a contact, it will update the description field of the contact record, get the contact owner’s email address, and (3b) send an email to the contact owner.
Steps:
1. Let’s create the before delete flow.
For those using Salesforce Classic, flow can be found in Create | Workflows & Approvals | Flows. In Lightning Experience, it is found under Process Automation | Flows. Click on “New Flow.” Select the Record-Triggered Flow and Next.
A. In the flow, we need to create a text template which text that we will use to updated the description field of either the account or contact record.
Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.
This is how that flow resource would be configured.
- Resource Type: Text Template
- API Name: DescriptionTT
- Data Type: Text
- View as Plain Text
- Body: {!$Record.Name} (a closed lost opp) was deleted by {!$User.FirstName} {!$User.LastName} on {!$Flow.CurrentDateTime}.
B. Select the Edit link in the Start element for the Trigger and select “A record is deleted.”
C. Next, select the Edit link for the Object in the Start element. Configure as follows:
- Object: Opportunity
- Condition Requirements: All Conditions Are Met (AND)
- StageName Equals Closed Lost
D. We need to decide whether the opportunity is associated only to an account or contact using a Decision flow element called Account or Contact Association.
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- Outcome: Account
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- $Record > Account ID Is Null False
- Outcome: Contact
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- $Record > Contact ID Is Null False
E. If the account outcome evaluates to true, we now want to update the account record using the Update Records flow element called Update the Account to update the description field that a closed opportunity in closed lost stage was deleted by the user’s name on <date time stamp>.
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- How to Find Records to Update and Set Their Values: Specify conditions to identify records, and set fields individually
- Object: Account
- Condition Requirements for Records to Update: All Conditions Are Met (AND)
- Id Equals $Record>Account ID
- Description: DescriptionTT
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- Body: {!$Record.Name} (a closed lost opp) was deleted by {!$User.FirstName} {!$User.LastName} on {!$Flow.CurrentDateTime}.
- Subject: Attention: The {!$Record.Name} (a closed lost opp) associated to your account was deleted
-
Email Addresses (comma-separated) – Toggle to Include: {!$Record.Account.Owner.Email}
G. If the contact outcome evaluates to true, we now want to update the contact record using the Update Records flow element called Update the Contact to update the description field that a closed opportunity in closed lost stage was deleted by the user’s name on <date time stamp>.
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- How to Find Records to Update and Set Their Values: Specify conditions to identify records, and set fields individually
- Object: Contact
- Condition Requirements for Records to Update: All Conditions Are Met (AND)
- Id Equals $Record>Contact ID
- Description: DescriptionTT
H. Next, since the related contact owner’s email address is not available via the $Record resource, we need to get the information from the contact record. This is done using the Get Records flow element called Get the Contact Owner’s Email.
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- Object: Contact
- Condition Requirements: All Conditions Are Met (AND)
- Id Equals $Record> Contact ID
- How Many Records to Store: Only the first record
- How to Store Record Data: Automatically store all fields
I. Similar to the account, we need to configure the Send Email action with an Action flow called Send Email to Contact Owner to send an email to the contact owner associated to the opportunity that a closed lost opp was deleted.
Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.
Configure to match the below:
- Body: {!$Record.Name} (a closed lost opp) was deleted by {!$User.FirstName} {!$User.LastName} on {!$Flow.CurrentDateTime}.
- Subject: Attention: The {!$Record.Name} (a closed lost opp) associated to your account was deleted
-
Email Addresses (comma-separated) – Toggle to Include: {!Get_the_Contact_Owner_s_Email.Owner.Email}
J. Connect the flow elements to match the below.
K. Save/Save As and provide the following properties.
Best practice tip: Provide a description so you and other/future admins know what this flow is used for.
L. Click the “Activate” button.
Now, before you deploy the changes to Production, don’t forget to test your configuration changes.
Deployment Notes/Tips:
- Flow can be deployed to Production in a change set (or can be deployed using a tool such as Metazoa’s Snapshot).
- You will find the flow in a change set under the Flow Definition component type.
- Activate the flow post deployment as flows deploy inactive in Production, unless 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 cover at least 75% of the total number of active processes and active autolaunched flows in your org or you can select 0%, which will run the apex classes not related to your flow.
Jen, thanks for this post! Is there a way to force an error message and stop the delete from being completed with this new feature?
LikeLike
You cannot use a before delete flow to prevent a record from being deleted like you can with a Before Delete trigger.
LikeLike
Hi Jenwlee,
is there away to restrict the deletion when a certain criteria is true?
Thanks,
Andras
LikeLike
As I noted in the blog post, you cannot use a before delete flow to prevent a record from being deleted like you can with a Before Delete trigger.
LikeLike
Jen- thanks for another practical use case, and very well explained too! I followed your example and built a flow to conditionally notify all our admins when a user deletes an Opp that either doesn’t belong to them or was created older than yesterday. (We noticed that most deletions are “oops, I didn’t mean to create that record” and we’re not so concerned about those.) Before creating this Flow, we were reluctant to allow non-admin users to have delete permissions, but now we’ve got it covered. Thanks again.
LikeLike
Glad to be of help!
LikeLike