4

As per trailhead module :

Bulk Apex Triggers

trigger SoqlTriggerBulk on Account(after update) {  
    // Perform SOQL query once.    
    // Get the related opportunities for the accounts in this trigger,
    // and iterate over those records.
    for(Opportunity opp : [SELECT Id,Name,CloseDate FROM Opportunity
        WHERE AccountId IN :Trigger.New]) {

        // Do some other processing
    }
}

Triggers execute on batches of 200 records at a time. So if 400 records cause a trigger to fire, the trigger fires twice, once for each 200 records. For this reason, you don’t get the benefit of SOQL for loop record batching in triggers, because triggers batch up records as well. The SOQL for loop is called twice in this example, but a standalone SOQL query would also be called twice. However, the SOQL for loop still looks more elegant than iterating over a collection variable!

I am processing 30K records and my trigger will be invoked 150 times.

Will I get SOQL 101 queries error if my trigger is invoked 1000 times?

6

Yes, you will get the 101 query exception1.

The reason is that all of the trigger "chunks" (I prefer using "chunk" over "batch", because "batch" in Salesforce is a separate thing) are part of the same transaction, and governor limits are enforced per-transaction.

If you use one query in your trigger, then your trigger can handle (at most) 20,000 records.

The number of records that you can handle decreases as your trigger becomes more complex (more queries and interactions with other triggers, workflow rules, or process builder).

+edit:
Of course, the other limit that you need to worry about is the DML rows limit, which is a hard cap of 10,000 records per transaction.

1: Well, you would if Salesforce didn't restrict the number of DML rows. If you try to dml 30K records, I'd expect you to receive a DmlException for the number of DML rows before you'd get a QueryExceptionfor the number of queries

  • Yeah, I was just testing by adding a query and not doing DML, but you are right DML limit is also a constraint and Max we can update 10000 rows – C0DEPirate Apr 25 '18 at 13:25
2

Short answer is No you will not get this error.

You can not update 30K records in single transaction so you need to break it in chunks that will also reset the SQL Limits. So in that case you will not get this error. For example Data loader (default) update record in chunk of 200 so you will never face this error in that case.

  • Tushar my friend, you are wrong here, I just tested it. Also check @derek answer, he is right we can only process up to 20K records – C0DEPirate Apr 25 '18 at 13:20
  • I would like to agree with Tushar. No you wont get 101 soql error because SF's Hard limit will kill it before. – Pranay Jaiswal Apr 25 '18 at 17:11
  • @codePirate As I have mention earlier there is other limit (DML) which will not allow to do this. So sorry but I don't agree with you. – Tushar Sharma Apr 26 '18 at 4:59
  • Yep, we ge the error after 10K and that too related to DML – C0DEPirate Apr 27 '18 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.