Find your content:

Search form

You are here

Test for delete success within Trigger context


I have a trigger use case that goes as follows: 1. On a junction object that joins two records of the same sObject type. 2. Junction object records reflects one direction of relationship (parent-to-child). 3. If a parent-to-child record gets created, a second of child-to-parent must also get created. 5. This creates pairs of records for every relationship. 6. If deleted, undeleted, perform like action on reciprocal junction object record.

My trigger is responsible for keeping these two paired records in sync and I'm doing it by writing an identical value to a field in each record, lets call this a "twin-key".

So on deletes, when one record is about to be deleted, I collect the twin-keys into a set. I then query for their pairs and delete what I find. But I want to only add to this set, if I can be assured that the delete of the one record will be successful.

So here I finally get to my question:

trigger myTrigger on my_obj__c (after delete)
for (my_obj__c m : Trigger.old){
   System.debug(m.isDeleted); //is false...makes is the old value!
System.debug(; //is null...not available in delete context!

Is there any way in the delete context to detect that a member of Trigger.old will be successfully deleted from the database? I looked at the isDeleted standard field, but this appears 'false' in the delete context. Ostensibly, if existed in the delete context, this would tell us. Am I missing something?

Attribution to: pchittum

Possible Suggestion/Solution #1

But I want to only add to this set, if I can be assured that the delete of the one record will be successful.

Do you have any other delete-related triggers that would prevent you from deleting? Surely logically they'd be firing on "before delete"?

I'd say that whole operation falls into normal transactional logic so you don't have to do anything special. If there would be at least 1 failed delete everything is rolled back.

If somebody then cheats by using allOrNone parameter on Database.delete(), in DataLoader etc. it should be the concern of this external actor to do it properly in 2 steps - check the delete results to learn what went OK and what failed, delete twins.

If you really want to keep it in a trigger I guess you can kick-off a @future with all Ids from trigger.oldMap.keyset(). In the method query for what was deleted ok and what stayed in the database [SELECT Id, isDeleted FROM Object__c WHERE Id IN :mySet ALL ROWS] and try to delete twins there?

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content