2

I am writing a trigger to handle after insert,after update and after delete events. I am using trigger.old for after delete.But, how to differentiate the deleted record from the rest of the records of the object?

What I have done so far is :

List<Object__c) lstTriggerOld = trigger.old;
List<Object__c> lstAllRecords = [SELECT Id FROM Object__c WHERE ID != NULL];
List<Object__c> deletedRecords = [SELECT Id FROM Object__c WHERE ID IN :lstTriggerOld AND ID NOT IN : lstAllRecords ];

But the last list is not fetching any rows.

Can someone help?

3

To get or Query deleted records you need to use ALL ROWS So your query will be

List<Object__c> deletedRecords = [SELECT Id FROM Object__c WHERE ID IN :lstTriggerOld AND ID NOT IN : lstAllRecords ALL ROWS];

All Rows Keyword

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.