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
Pankaj Mishra 39Pankaj Mishra 39 

Apex Trigger see only 200 records in one chunk

I am aware that Trigger can process in chunks of 1-200 records at a time, is there any way we can increase this record count?

Here is my case: I am inserting 10,000 records from Test class
and my Trigger has 4 SOQL queries, as Triggers are processed in chunks of 200 records, my Trigger will be fired 50 times in this case. However, in each call my SOQL will get accumulated towards SOQL limits and Trigger will fail when it is called 26th time with SOQL limit exception.

In my Trigger I need to have 4 SOQL anyhow and I cannot reduce that, I know that Batch is the best solution in such scenarios but my application need this processing in real time.

Any solution or workaround is highly appreciated.
 
Tim BarsottiTim Barsotti

You can not increase the number of records executed in a single execution context.

Why do you need to insert 10k records for a test class? A batch of 200 should suffice in testing all of the business logic of your code.

Pankaj Mishra 39Pankaj Mishra 39
We have a requirement where in there could be possibility that 10,000 records are going to be inserted in one go, so Trigger will fail for SOQL limits. I figured out this issue while doing Load testing with 10,000 records in test class.
Tim BarsottiTim Barsotti

Write the test class to cover 200 records at a time. No need to go beyond that. "Any import processes you currently have running should continue to work with the Bulk API as it uses the same save logic and has the same processing side-effects as the SOAP API. To stay within the current governor and limits, records are processed 200 at a time from your batches. So if your triggers are written to process 200 records successfully you should be fine."

https://developer.salesforce.com/page/Loading_Large_Data_Sets_with_the_Force.com_Bulk_API

Pankaj Mishra 39Pankaj Mishra 39
Well, the bulk load is not the issue and my Trigger is bulkified as well...the issue is if Trigger is called multiple times in one transaction then I am hitting SOQL limits.
Pankaj Mishra 39Pankaj Mishra 39
Also, the document you shared talks about Bulk data load via API which is not the issue here.
Tim BarsottiTim Barsotti

Pankaj, how is your user going to insert / update 10k records in one execution context? 

Have you considering using singletons to prevent re-querying every batch executed in the test class?