0

I am trying to test a trigger that needs to update a field in an object.

I am inserting 10,000 entries so I expect the trigger, which runs over a "before insert", to update a particular field, in this case, no DML required, since using "before insert".

I am checking if all the entries are updated but only 200 entries are updated.

Is this a limitation is salesforce test environment?

  • 5
    Triggers get executed in batches of 200. So if you insert 10000 entries, the trigger is run 50 times. If you check for some condition in a static Boolean somewhere, a commonly used way to prevent triggers from calling each other, that will only work the first iteration. – rael_kid May 22 '17 at 12:56
  • 1
    @rael_kid Your comment looks like it would be a serviceable answer – Derek F May 22 '17 at 13:55
  • I've elaborated a bit more and posted an answer. – rael_kid May 22 '17 at 19:16
2

Triggers get executed in batches of 200; in your case, this means that Trigger.new will never be larger than 200. If you check for some condition in a static Boolean somewhere, that will only work the first iteration.

So if you insert 10000 entries, the trigger is run 50 times. A commonly used way to prevent triggers from calling each other or calling themselves again is to use a static Boolean, since their values persist between different iterations that are the result of all DML executions in the same context.

For instance, suppose you have a class called TriggerExample that contains public static Boolean triggerExecuted = false;. Now consider in your trigger you do something like this:

if(!TriggerExample.triggerExecuted){
    for(Account a : Trigger.new){
        //set some fields of a
    }
    TriggerExample.triggerExecuted = true;
}

This means that in the first iteration (ie. the first 200 rows) of the trigger the Boolean will be set to true and no other iterations (even from the same DML call) will enter the if-block.

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.