0

I have a need to kind of customize the deletion of a custom object. The deletion need to update a checkBox to true instead of really delete the record.

For that I tried many things using trigger before and after delete. First I tried to simply add Error and update the record on before delete but since the add Error doing a roll back, the update does not work.

Now I am trying 2 others way and I don't know which is the best or if there is an other way to do that.

  1. Clone the record before delete and insert it with the checkBox updated to true.
  2. Update the record and undelete it

What is the best way to do that?

4
  • Can't you use a Lightning Action which do a field update on the record? But you will see a modal to prompt to change the field (your checkbox for example)
    – Kevan
    Commented May 23, 2021 at 9:01
  • Thanks for your answer but I also need to handle the case where the record is deleted from apex or from API for example.
    – Raphael D
    Commented May 23, 2021 at 9:06
  • For Apex, it is quite simple, instead of doing a delete, simply do an update on the checkbox for your soft destroy. Regarding API, you can try to expose a webservice instead of standard API.
    – Kevan
    Commented May 23, 2021 at 9:07
  • What I need is one solution for all the possibilities, which is on delete trigger.
    – Raphael D
    Commented May 23, 2021 at 10:02

1 Answer 1

1

If you never want the deletion allowed, add a trigger that creates an immediate publish behaviour platform event with a field that can store the record's ID and have the trigger add an error to the record itself (thereby disallowing the deletion). You then have a trigger-based platform event subscriber that simply loads the record(s) for the received events and updates those record(s) with your boolean flag.

This will work because the immediate publish behaviour means the event is raised regardless of the outcome of the originating transaction.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .