0

Our previous Salesforce developer used the below 'first-call pattern' in every Apex trigger.

trigger ContactTgr on Contact (before insert) {
    public Static Boolean firstcall = True;
    //Test to see if this is the first call to this trigger
    if (!firstcall) {
        return;
    }   
    // actual trigger code..
    
    firstcall = false;
}

I wonder if this still applies. Is this pattern outdated, should it be used for certain types of triggers, or is actually a good idea?

1
  • 3
    cf. this excellent question. This particular static-Boolean pattern is poison.
    – David Reed
    Feb 23, 2021 at 18:35

1 Answer 1

3

If you decide you need recursion prevention, this approach is still the wrong way to go. When you use a Boolean to track first vs. subsequent entry, you will always prevent bulkification beyond 200 records. Use of Set<Id>, one per event, would be a much better pattern. However, in general, it is better to use proper filtering (especially making sure the relevant fields changed) before executing actions against trigger context records.

1
  • 2
    Thanks! I had a hunch that this was pattern was just a band-aid and now it turns out it is actually bad for bulkification. I think I have to review the record updates more closely and better remove this pattern altogether.
    – wp78de
    Feb 23, 2021 at 19:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .