+ Start a Discussion
Kodan HuntKodan Hunt 

How to differentiate between manual delete and code wise delete of a task in Apex trigger



 I have a trigger which is basically to restrict the user from manually deleting a task in salesforce. And also the same task can be delete by Apex code. Say like if I'm creating a simillar task then the already existing task should get delete and the new task has to get create. So how to differentiate the request between whether that particular trigger is occuring from salesforce page or from another apex code??


Thanks in advance !!






I don't think there's any way that you can reliably detect where the request is coming from.


The way we've handled this in the past is to have a checkbox field that is hidden from the UI, named "delete override" or similar, and the Apex code sets this to true prior to executing the delete.  The trigger then checks the value of this field and if it is true, the delete originated from apex code and is allowed.


In the apex class add a static field named isDeleteManual. Set the default value as true.

Now right in front of the apex code block where the delete is supposed to occur  set its value to false.

Use this isDeleteManual in your trigger to control your delete. Then in the trigger after delete set the isDeleteManual to true.


It is just about four lines of code split between your apex class and the trigger.


Kodan HuntKodan Hunt
Thanks dipu.. Great idea !! It resolved my issue.