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
Gyanendra Singh 8Gyanendra Singh 8 

Bulk Record processing in Trigger

Ok we all know that 'Apex triggers are optimized to operate in bulk, which, by definition, requires developers to write logic that supports bulk operations' (Copied from salesforce) .

Now let's take a scenario, say I have a custom Object named MyCustomOb_c and I have written an after insert trigger on it which does something like field update etc. Now my question is what are the scenarios when the trigger would be invoked for bulk record processing ? I can think of one. If inserting say 20 records through data loader in the MyCustomOb_c.

What else?
If my organization has lets say 70 users and out of 70 let's assume 40 independent users inserts 40 records in this custom object (1 record per user) so does all the 40 records would be processed in one single trigger invocation? Or in other words the Size of the Trigger.New would be 40? If not then what else? I mean how it would be decided how many records would go in one single trigger invocation? Does the time interval in which these 40 records are inserted would matter?

Hope I am clear. I am just trying to figure out the scenarios and trying to figure out how many records at one go would go into the trigger and how it is decided?

Thanks in advance.

MissedCallMissedCall
Hello there,

Each individual user's record insert is single trigger invocation.

If single user is trying to insert more than 1 record at a time using api's, then it is considered as bulk processing where the records are inserted in a batch size of 200.
Gyanendra Singh 8Gyanendra Singh 8

So if that is the case then we don't have any other scenario where bulk record processing would happen apart from Data Loader or any other method using API? .

So if my organization never uses data loader or any other API for record insertion and say there are 300 users in my organization and they do this record insertion then if I write the trigger to operate in Bulk or to operate for a single record it is irrelevant ?

MissedCallMissedCall
It is irrelevant. But you should always follow the best practises and you never know what might come up in the future. So code should always be designed to handle bulk processing.