You need to sign in to do that
Don't have an account?
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.
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.
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:
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
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:
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.