Wednesday, 15 August 2012

I have 1000 records, how can I execute 200 records each time in Trigger?

By default triggers will run in a batch of 200 records. For 200 record one trigger invocation, even in case of Bulk API.

Consider a scenario where you're inserting 1100 records then it will be run in 6 batches

200 * 5  + 100 * 1 = 6 (Invocations)


* Use SOQL for loops to operate on records in batches of 200.
* SOQL for loop, which iterates over the returned results in batches of 200 records.
* This reduces the size of the ml list variable which now holds 200 items instead of all items in the query results, and gets recreated for every batch.

for (List<Merchandise__c> ml : [SELECT Id,Name FROM Merchandise__c])
// Do something.

* You can also use batch apex class to process large number of records.

No comments:

Post a Comment