function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lonedeveloperlonedeveloper 

Apex Job not running asynchronously in Trigger

Hi,

 

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 using Salesforce interface.  When I run the same Apex Job on the same number of records (15,000) using VisualForce page runs instantly, the 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:

 

Screen shot of Apex Jobs page

 

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 (at least according to the user interface) and based on Submitted and Completed date the job is not Asynchronously. 

 

Apex Job is queued like this:

 

BatchDeleteResults batch = new BatchDeleteResults();
batch.deleteQuery = deleteQuery;
    	
ID batchId = Database.executeBatch(batch);

 

Has anyone else experienced something like this?

 

Thanks,

lonedeveloperlonedeveloper

I still haven't been able to figure out a reason why Apex Job is not running asynchronously.

 

This is the trigger (object names changed but the logic is same)

 

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;
}

 

and 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){

}   
}

 

Anyone know what the issue could be?

 

Thanks.