9

As far I know, while processing bulk operations in Trigger, the data is split in chunks of 200 per batch. I have around 40K accounts in org and I wanted to delete around 1000 of them. To understand limits, I wrote a simple after trigger with limits check in it.

trigger Account_After on Account ( after delete, after insert, after undelete, after update ) {
    if(Trigger.isDelete){        
        System.debug('Number of records executed in this batch: ' + Trigger.old.size());
        System.debug('Returns the total number of SOQL queries that can be issued.: ' +  Limits.getLimitQueries());
        System.debug('Returns the number of DML statements (such as insert, update) that have been called.: ' + Limits.getDMLStatements());
        System.debug('Returns the number of SOQL queries that have been issued.: ' + Limits.getQueries() );
    }
}  

I executed the code anonymous:

List<Account> A = [Select Id from Account LIMIT 1000];
Database.Delete(A, false);

I expected, this trigger to run 5 times in batch of 200 each; however the output I received was one batch with 1000 records.

15:35:05:125 USER_DEBUG [95]|DEBUG|Number of records executed in this batch: 1000
15:35:05:126 USER_DEBUG [96]|DEBUG|Returns the total number of SOQL queries that can be issued.: 100
15:35:05:126 USER_DEBUG [97]|DEBUG|Returns the number of DML statements (such as insert, update) that have been called.: 1
15:35:05:126 USER_DEBUG [98]|DEBUG|Returns the number of SOQL queries that have been issued.: 3

Why the records got deleted in one batch, or my understanding of bulk trigger is wrong?

4
  • Not that it makes much difference, but can you clarify if you ran this code before, after, or both?
    – Adrian Larson
    Mar 7, 2018 at 15:19
  • Please edit your question with any clarifications. That's not what comments are for.
    – Adrian Larson
    Mar 7, 2018 at 15:24
  • I'm asking around. It should not be behaving this way, as far as I recall.
    – sfdcfox
    Mar 7, 2018 at 16:32
  • Any update Gurus??
    – Dev Arya
    Mar 8, 2018 at 12:47

1 Answer 1

0

I just done some basic POC and Here is the result which I have found.

In case of Insert/Update Salesforce split the records in the chunk of 200 and process them. WHile in case of delete it process them in single execution.

The reason might be when you we perform Insert or update. There might be some Workflow, Process builder, Approval Process, Validation rule, Rollup field calculations (The complete trigger order of execution) runs as well. This increase the CPU process time. So to save some time salesforce split them in chunk so that it can process them easily.

But in case of delete only trigger fire(Which are not always available) and Rollup field calculations. So there is not much extra time required.

So instead of splitting records in chunk salesforce process them in single transaction.

When we perform Insert/Update

enter image description here

enter image description here

But in the case of delete Salesforce process all records in a single chunk enter image description here

4
  • I understand your hypothesis, off-course bulk delete operations are not so common but an explain for the bulk delete behavior is completely missing. I investigated a little further, with some operations with in the delete trigger, for example updation of contacts: if (Trigger.isDelete) { List<Contact> lstContacts = [SELECT Id FROM Contact WHERE AccountId IN :Trigger.old LIMIT 201]; if (!lstContacts .isEmpty()) { System.debug('Update Contacts: '+lstContacts.size()); update lstContacts ; } }
    – Dev Arya
    Mar 8, 2018 at 12:16
  • @DevArya and what did you found? Mar 8, 2018 at 12:19
  • The update or insert statements made from primary delete operation, are handled in chunks of 200 record per batch, but delete is executed at one go. imgur.com/a/pYssP I am still looking for more valid explanation from Salesforce side.
    – Dev Arya
    Mar 8, 2018 at 12:45
  • @DevArya yes that I have already checked. May be some inside guy can help us here. Mar 8, 2018 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .