Winter '21 | Before Delete Flow - Hands-on
In this article we will see the capability of Before Delete
Record-Triggered Flows. Before Delete Flow is the latest addition to the Record Triggered Flow capability
and is introduced recently in Salesforce Winter '21 release.
Let's checkout this latest capability with a sample Use Case.
Before moving forward, if your org is old, you can sign up for the Winter '21 pre-release org here : https://www.salesforce.com/form/signup/prerelease-winter21/
Requirement and Scenario
Steps:
From Winter '21 we have option to use Free Form (existing) or Auto Layout option to create Flows.
3. Click the Edit on Start Element to choose Record Triggered Flow type.
Click on the Latest Option called "A record is Deleted".
Here's a Quick Demo :
Before Delete Record-Triggered Flow
As the name suggests this type of Flow will be triggered
or fired whenever a record is deleted. The importance of Before keyword is that this will be fired
before the delete operation is initiated and completed.
The details and all the Field Values of the Record that fires this Flow Trigger will be available in the $Record Global Variable throughout the Flow. This can be used to access or fetch any Record level details, related parent record details or related child records.
Also, unlike Before Insert and Before Update Flow
Triggers which only have Get Records Element available, we have all the four Data
Elements (Get,Create,Update and Delete) in Before Delete Flows. This enables us to perform these
operations on any set of records before the record that fired the Flow is actually
deleted.
Bulk Record Delete Handling: Flow Triggers are automatically Bulkified in a way that system will automatically Run the Flow for all the records being deleted in a Bulk Delete operation in batches/chunks of records. For example if we delete say 400 records at once using apex or batch etc. the Before Delete Flow will be fired two times (one for 200 records each). So anything that is written inside the Flow or any Element present inside the Flow will be called two times in this transaction.
Anything that we do or create inside this Flow has to be bulkified from our side so as to avoid hitting Governor Limits. For example, No Get, Delete, Update or Create Elements inside Loops.
The details and all the Field Values of the Record that fires this Flow Trigger will be available in the $Record Global Variable throughout the Flow. This can be used to access or fetch any Record level details, related parent record details or related child records.
Bulk Record Delete Handling: Flow Triggers are automatically Bulkified in a way that system will automatically Run the Flow for all the records being deleted in a Bulk Delete operation in batches/chunks of records. For example if we delete say 400 records at once using apex or batch etc. the Before Delete Flow will be fired two times (one for 200 records each). So anything that is written inside the Flow or any Element present inside the Flow will be called two times in this transaction.
Anything that we do or create inside this Flow has to be bulkified from our side so as to avoid hitting Governor Limits. For example, No Get, Delete, Update or Create Elements inside Loops.
No need to write Apex Code every-time we need
to perform any Before Delete Logic now and this will greatly help in reducing dependency on Apex
Triggers for before delete use cases.
Important:
1. Never do Flow testing/debugging in Production. Always test and debug Flows in a Sandbox.
Important:
1. Never do Flow testing/debugging in Production. Always test and debug Flows in a Sandbox.
Let's checkout this latest capability with a sample Use Case.
Before moving forward, if your org is old, you can sign up for the Winter '21 pre-release org here : https://www.salesforce.com/form/signup/prerelease-winter21/
Sample Use Case
In general, when we try to delete any Contact Records
which have one or more related Case records, system doesn't allow us to delete them. We get an error
message like "Contact cannot be deleted as it is associated to Cases ". Contacts can only be deleted
after we delete all related Cases first.
Now assume if we want to Mass Delete 100 contacts
which have related Case records. Seems like a lot of work. Previously the only valid option was to
write an Apex Trigger to delete related cases first before deleting a Contact. But now we have
Before Delete Flows too!
We will create a Before Delete Flow which will be fired whenever we try to delete Contact records and will automatically delete the related Cases first.
We will create a Before Delete Flow which will be fired whenever we try to delete Contact records and will automatically delete the related Cases first.
In our sample scenario, Assume there is Custom Field
(Boolean) on Contact Object named "Archived".
When we delete any Contact which has Archived as checked
or True, any related cases for that Contact must be deleted before contact is deleted so that system
does not throw the error as shown above.
User Deletes a Contact which has Archived = True -->
Flow will Fire --> It checks for any Related Cases --> If Cases Found --> Deletes All Related
Cases --> Since related Cases are Deleted and no more , system deletes the Contact
successfully.
How Will Before Delete Flow Trigger Solve this Problem?
Basically, this trigger will be fired in Before Delete context before the record is deleted from the system so we will use this Flow to delete the related Cases before the system tries to delete a Contact. Thus, the record will be deleted successfully without the error seen above.
How Will Before Delete Flow Trigger Solve this Problem?
Basically, this trigger will be fired in Before Delete context before the record is deleted from the system so we will use this Flow to delete the related Cases before the system tries to delete a Contact. Thus, the record will be deleted successfully without the error seen above.
Flow Design
1.
Create a New Flow of Type Record Triggered Flow.
2.
Select which Layout to use Free Form or Auto Layout (Beta).From Winter '21 we have option to use Free Form (existing) or Auto Layout option to create Flows.
3. Click the Edit on Start Element to choose Record Triggered Flow type.
Click on the Latest Option called "A record is Deleted".
4.
Select the Object from the Start Element and Define the Trigger/Fire Criteria.
In our
case we will Select Contact as our object since we need to Fire the Flow whenever we try to delete
any Contact Record.
Important:
From Winter '21 we can now fire the Flow Trigger based on conditions/criteria. This gives us
more control since the Flow will not be fired for the records which do not meet the specified
criteria and it makes the overall DML process a bit fast.
We
can define this criteria after selecting the Object for Flow Trigger.
In our
case the criteria is that the Flow should only fire if the custom field (Boolean) on Contact named
"Archived" is True/Checked for the Contact Record(s) being Deleted.
So we
will define this criteria after choosing the Object as Contact from the Start Element.
5.
Find Related Case Records Using a Get Record Element.
Now,
since our Flow will only fire if any Contact with Archived = True is deleted, in that
scenario we will first fetch/find all the Related Cases for the Contact that fired this Flow.
Since
we have the record details that fired the Flow in Global Variable $Record, we will just
use $Record.Id (Contact Id) as the filter condition to find all the related cases for
this contact record. Case records have a lookup field (ContactId), so all Cases related to
the Contact that fired this flow will have ContactId = $Record.Id, which is
basically the Id of the contact record being
deleted (Case.ContactId= $Record.Id)
The
name of this variable is important as it has to be used later in other Elements. Name
is {!Find_Related_Cases}
6.
Use Decision to Check Related Cases Found
Remember,
the only purpose of writing this Flow was to Delete Related Cases for the Contact being
deleted so that system will allow the contact to delete. If there are no Related Cases, the Contact
can be Deleted anyways normally.
So, we
only need to proceed further if any Related Cases are found for the Contact Record being deleted. If
not, we can simply stop here and do nothing.
Using
Decision Element we will check if the Get Records Element for Cases {!Find_Related_Cases} returned any
records or not.
7.
Use Delete Element to Delete Related Cases (if found)
If
Cases were found in Get Record Element only in that case, Flow will be able to pass the Decision
Element and reach the Last Element i.e. Delete Element.
In this
Delete Element, we will simply Delete all the Case Records that were found in the Get Element
{!Find_Related_Cases} as that list/data is already available to us in the Flow.
And we are done!
This Flow will Fire when a Contact is deleted and that contact has field value of Archived as True. Flow will delete any related cases if found. Since the Cases are deleted in before delete context, system will delete the contact successfully without throwing any errors.
Here is a quick Recap of the steps in a short Video
This Flow will Fire when a Contact is deleted and that contact has field value of Archived as True. Flow will delete any related cases if found. Since the Cases are deleted in before delete context, system will delete the contact successfully without throwing any errors.
Here is a quick Recap of the steps in a short Video
Here's a Quick Demo :
Hope this Helps! Reach out to
me here or on LinkedIn for any questions, issues or suggestions. Happy to Help!
Don't Miss Latest Posts
: CLICK HERE TO SUBSCRIBE TO THIS BLOG
Checkout this Article for all
Flow Related Scenarios : All
Flow Examples
Checkout all Flow Related Posts on this Blog. Click Here : Flow Posts : Use Cases and Scenarios
Thanks for Reading. See you soon with Latest Features!
Subscribe
Great use case and explanation!!!
ReplyDelete- Gidi
Thanks Gidi for the Feedback :)
DeleteI'm deleting a record from Opportunity Team Member. When a record is delete it from this object I need to update a field in its Opportunity. But it is not working.
ReplyDeleteIs there any consideration/limitation on Opportunity Team Member?
Thanks!
Hi, same issue on campaign members. Flow does not trigger on delete. Any ideas? Thanks
DeleteHi I am seeing a similar issue with Opportunity Product, it is not triggering the flow. Is this a limitation of the Opportunity Product object?
ReplyDeleteSame here! If I can get this to work, that'd be awesome. Would make it so that my amount fields would also adjust.
DeleteThank you very much!
ReplyDeleteIt is worked perfectly!
That was very helpful, specially the part before checking the records to delete. I was searching the same and got here. Thank you so much.
ReplyDeleteCan you please help with the Apex action for showing custom error message on deletion?
DeleteCan you please help with the Apex action for showing custom error message on deletion since there is auto-generated message appears during delete?
ReplyDelete