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
Felix Jong Seok ChaeFelix Jong Seok Chae 

What happens to value of class variable after trigger fires?

I have a static class variable called numRemain, which is initially set to some positive value and trigger decrements its value by 1 for each recursive call. Because my after-insert trigger has the part where each case in Trigger.New is updated, it fires Trigger in recursive manner. My queston is "after trigger is called more than once, is numRemain reset to its initial value or is numRemain decremented after each recursion?"
The reason why I ask this question is that I keep getting Apex Timeout error even though I have the conditional statement if(numRemain>0) in the beginning of the apex trigger.
Thank you. 
Best Answer chosen by Felix Jong Seok Chae
Alba RivasAlba Rivas
Hi Felix,

If after trigger is called more than once in different transactions (eg: you update a record, and then in a separate context you update a different one), the variable numRemain will be reset.

If the trigger is called more than once as part of the same transaction, it won't. This can happen mainly because of two reasons:

- You have workflow rules or processes that are updating the record and firing the triggers again
- You are inserting/updating more than 200 records at a time (then the trigger executes in chunks of 200)

As you are describing your problem, it seems you are doing something like this:
 
public class AccountTriggerHandler {
    
    public static Decimal counter = 0;
    
    public void handle()
    {
        if (counter>0)
        	return;
        
        counter++;
        
        Account acct = new Account();
        acct.Name = ' ' + counter;
        insert acct;        
    }
}

In this case, counter is correctly incremented and I get only 2 accounts inserted, the one that fired the trigger and the one I create in the after insert (Bear in mind I only call this handler on the after insert part).

Hope that helps.

Regards.

All Answers

Alba RivasAlba Rivas
Hi Felix,

If after trigger is called more than once in different transactions (eg: you update a record, and then in a separate context you update a different one), the variable numRemain will be reset.

If the trigger is called more than once as part of the same transaction, it won't. This can happen mainly because of two reasons:

- You have workflow rules or processes that are updating the record and firing the triggers again
- You are inserting/updating more than 200 records at a time (then the trigger executes in chunks of 200)

As you are describing your problem, it seems you are doing something like this:
 
public class AccountTriggerHandler {
    
    public static Decimal counter = 0;
    
    public void handle()
    {
        if (counter>0)
        	return;
        
        counter++;
        
        Account acct = new Account();
        acct.Name = ' ' + counter;
        insert acct;        
    }
}

In this case, counter is correctly incremented and I get only 2 accounts inserted, the one that fired the trigger and the one I create in the after insert (Bear in mind I only call this handler on the after insert part).

Hope that helps.

Regards.
This was selected as the best answer
Alba RivasAlba Rivas
I forgot to say that in the code case, everything is the same transaction or execution context, so the counter won't be reset between the 1st and 2nd trigger execution.
Felix Jong Seok ChaeFelix Jong Seok Chae
Thanks Alba.