function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Serena SchultzSerena Schultz 

Launch headless flow from trigger before delete

Hello All!

I’m a fairly proficient declarative admin with minimal Apex writing knowledge. I tend to avoid it whenever necessary. However, I have a requirement where I need to run an automation before delete on a custom object. I have this handled from the record page (I’ve created a custom Delete button that calls a flow to evaluate if the record can in fact be deleted and the updates the user accordingly if it is not allowed via a screen pop-up). 

However, there is still a loophole where users can delete from list views or related lists since I cannot seem to figure out how to swap out that delete button.

My workaround idea is to use a Before Delete Apex trigger to launch a headless version of the same flow to evaluate and delete the record (or not). 

Below is the code for my Apex trigger. It doesn’t show any errors from the developer console, but honestly I’m not 100% sure how to test it. 

My hope is that it will run before delete to call the flow (which works well when called via debug mode) and either complete the delete or cause a failure on the delete (which I can then send notifications or email alerts from). When I’ve tested this the records just delete (even when the flow shouldn't allow them to) so I'm not sure everything is running. I’m not sure if it’s an issue with my code or with my scenario. If I run a flow before delete will it disrupt the deletion or just run before the delete happens?

Thanks in advance!

Apex code example