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
Prashant.SakhujaPrashant.Sakhuja 

Help with error: "Total number of records processed as a result of DML cannot exceed 100."

Hi,

I have a requirement that I have implemented using apex trigger but need some help to make it scalable.

 

Requirement:

Auto generate Order Line Items when a Order record is created. Count of Order Line Items depends on the attributes of the Order and can vary from 20 to 1500.

 

My Solution:

Apex trigger on Order object that reads the attributes from the record and uses that in SOQL query to generate Order Line Items.

 

Issue #1:

When there are more than 1000 records returned I get an error. to avoid that I have a LIMIT 1000 added to the SOQL statement. However that still is an issue because I dont know which records I am missing from the result set.

 

Issue #2:

When the number of Order Line Items to be created is less than 100 the trigger works fine.

But when the number of Line Items is 100+ (there is only record that enters the Trigger, so there is no scaling of the governor limit.)

I use Limits.getDmlRows() to prevent run time error, but that does not still allow me to create more than 100 Line Items.

 

What I am running into is really two governor limits. The limits scale with record cound but since only 1 record is created new, the limits dont scale.

1. Total number of records processed as a result of DML cannot exceed 100

2. Total number of records retrieved by SOQL queries cannot exceed 1000

 


Expected Solution:

In the Ideal scenario, if based on the Order- 1420 Line Items are required, then the system should be able to generate that whenever such Order record is created.

 

Any help or pointers on this will be appreciated. Thanks in advance!!

 

 


 

Best Answer chosen by Admin (Salesforce Developers) 
Prashant.SakhujaPrashant.Sakhuja

Ama, Sarge, 

Thanks for your inputs. I was also toying with the idea of batch but was reluctant to implement cause it was not practical to make users wait before they could see the results.

 

After speaking with Sid Bansal from Salesforce.com I was convinced that @future is a better way. I now have it implemented and running as intended. There is a delay in processing with @future compared to trigger but not noticeable since my data volume is in hundreds and low thousands.

 

@future methods do not accept sobjects as inputs. My workaround for that was to extract the Id as List at the trigger level and then pass it to the @future method.

 

NOTE: Not sure why I never recd. the notifications for your replies even though I was subscribed to this thread.

 

Thanks again for your help!

All Answers

amar joshiamar joshi

Looks like u r not processing ur trigger in bulk.or may be ur SOQL or DML is in for loop pls send the code so can find the excet problem.

SargeSarge

Hi Prashant,

 

    From your requirement it seems like you need to use batch apex for atleast one of your uses cases.

 

E.g. Say you insert a Order record and it is learned that you need to insert 1500 order line items at one strech. The problem here is that using the trigger you cannot have more than 100 order line items to insert when you have 1 Order record processed by the trigger. So you should submit this job to some batch in order that you dont hit governor limits. (Apex class executed outside trigger's context has a limit of 10,000 records for insert).

 

   Here is the link for batch apex in documentations.

 

Note:   

 

The two governor limits you addressed scale up with number of records processed by the trigger.

 

for 1) Total number of records processed as DML statement in current trigger =  Trigger.new.size() * 100;

      2) Total number of records retrieved via SOQL in current trigger = Trigger.new().size*1000;

 

Hence it really depends on how many records you have in trigger.new.size() to determine how much records a DML staement can process or how many records can be retrieved by SOQL. So if it requires only < the limit then you can process order line items in trigger else process as a job or as a batch using batch apex.

 

 

Cheers...

Prashant.SakhujaPrashant.Sakhuja

Ama, Sarge, 

Thanks for your inputs. I was also toying with the idea of batch but was reluctant to implement cause it was not practical to make users wait before they could see the results.

 

After speaking with Sid Bansal from Salesforce.com I was convinced that @future is a better way. I now have it implemented and running as intended. There is a delay in processing with @future compared to trigger but not noticeable since my data volume is in hundreds and low thousands.

 

@future methods do not accept sobjects as inputs. My workaround for that was to extract the Id as List at the trigger level and then pass it to the @future method.

 

NOTE: Not sure why I never recd. the notifications for your replies even though I was subscribed to this thread.

 

Thanks again for your help!

This was selected as the best answer