I am running Apex Job in before delete trigger and noticed the deletion of a single Campaign through Salesforce interface takes around 34 seconds and Apex Job has completed running when the Campaign is deleted. When I run the same Apex Job on the same number of records (15,000) using VisualForce page, Apex Job is queued and runs after the VisualForce page has completed.
In this screen shot the first job was queued using VisualForce and the second and third were queued using trigger:

As you can see although the same number of records (15,000) are processed by the same Apex Job when it is queued using Trigger no batches are created and Submitted and Completed date are quite different.
Apex Job is queued in trigger:
trigger DeleteChilds on Campaign (before delete) {
public static final Integer MAX_DELETED = 500;
Set<ID> ids = new Set<Id>();
List<Parent__c> objs = new List<Parent__c>();
for(Campaign c : Trigger.old)
ids.add(c.Id);
objs = [SELECT Id FROM Parent__c WHERE Campaign__c IN :ids];
ids = new Set<Id>();
for(Parent__c s : objs)
ids.add(s.Id);
Integer childCount = [SELECT count() FROM Childs__c WHERE Parent__c IN :ids];
if(childCount < MAX_DELETED){
List<Childs__c> childs = [SELECT Id FROM Childs__c WHERE Parent__c IN :ids];
delete childs;
} else {
String deleteQuery = 'SELECT Id FROM Childs__c WHERE ';
for(ID id : ids){
deleteQuery += String.format(' Parent__c = \'\'{0}\'\' OR', new String[] {id});
}
if(deleteQuery.endsWith('OR')){
deleteQuery = deleteQuery.substring(0, deleteQuery.length() - 2);
}
BatchDeleteChilds batch = new BatchDeleteChilds();
batch.deleteQuery = deleteQuery;
ID batchId = Database.executeBatch(batch);
}
delete objs;
}
This is the Apex Job:
global class BatchDeleteChilds implements Database.Batchable<sObject> {
public String deleteQuery;
global Database.QueryLocator start(Database.BatchableContext context){
return Database.getQueryLocator(deleteQuery);
}
global void execute(Database.BatchableContext context, List<sObject> records){
delete records;
DataBase.emptyRecycleBin(records);
}
global void finish(Database.BatchableContext context){
}
}
Has anyone else experienced something like this?
Thanks,
You’re looking at a race condition which you’ll probably never win. The parent objects that have been deleted will have been committed by the time your batch job executes which nulls out the lookup from child to parent in the database. Because of this the query you pass into the batch will have zero records.
I don’t know the design decisions that motivated this approach, but there are other options available to mass delete child records. The simplest would be to have the child to parent relationship be a master detail. Child records are automatically deleted with this type of relationship.
Secondarily, you could override the delete button for the parent object with a visualforce page. The controller would then count the children and decide whether to launch a batch process to delete them. Then have the batch process wait to delete the parent object until it’s finish method.