Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8205255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:09:52+00:00 2026-06-07T08:09:52+00:00

I am running Apex Job in before delete trigger and noticed the deletion of

  • 0

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:

Apex Jobs

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:09:54+00:00Added an answer on June 7, 2026 at 8:09 am

    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.

    • Pre-Delete Child: (name = ‘example’, parent__c = ‘123’)
    • Post-Delete (Pre-Batch) Child: (name = ‘example’, parent__c = null)

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Running Solr on Tomcat 7 on Win 2008 Server. I am looping through a
Running my script through Devel::NYTProf showed that the following portion of code took up
Running my finished Rails Tutorial app through Rails Best Practices got me a warning
Running this query takes a long time: SELECT host,ip FROM arecords WHERE ip IN
Running on MySQL 5.5.9 with InnoDB. I created the following trigger: CREATE TRIGGER TRIGGER_Products_Insert
Running VS2003, nHB 3.0.0.3001, trying to run a unit test through Gallio 3.2. The
Running Rails 3.2.8, devise 2.1.2. I noticed that the migration contains blank defaults: class
I am a relative APEX noob. I'm running APEX 4.0 against a 10gR2 database.
I'm currently learning apex (using the Force.com IDE), and I'm running into some trouble
I'm running the following sql query in my Oracle Apex application: SELECT column1 ||

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.