6

As per this documentation Trigger

Note that static variable values are reset between API batches, but governor limits are not. Do not use static variables to track state information between API batches

I have this trigger

trigger apextrigger on Account (before insert) {

    system.debug('Counter is'+ myclassfortrigger.incrementcounter());
    system.debug(Trigger.new.size()+'MYTriggernew');
}

and class

public class myclassfortrigger{
public static integer counter = 0;      
    public static integer incrementcounter()
    {

        counter = counter + 1;
        return counter;
    }

    }

when i pass say 500 record to this,via Dataloader,Bulk api enabled and batch size is made as 500

In my debug log i can see one log for this(as expected) and inside in my debug statements i could see static variables are not getting reset

it shows

16:17:39.128 (128115231)|USER_DEBUG|[3]|DEBUG|Counter is1 16:17:40.795 (1795145037)|USER_DEBUG|[3]|DEBUG|Counter is2

As it uses API batches i thought as per documentation static variables would have got reset?

Where am i going worng?

1

2 Answers 2

2

This documentation appears to be bogus. The entire quote is:

For Apex saved using Salesforce.com API version 20.0 or earlier, if an API call causes a trigger to fire, the chunk of 200 records to process is further split into chunks of 100 records. For Apex saved using Salesforce.com API version 21.0 and later, no further splits of API chunks occur. Note that static variable values are reset between API batches, but governor limits are not. Do not use static variables to track state information between API batches.

However, during testing various sizes and API modes, I was never able to achieve a trigger size of 100. Even running in v10.0 mode, the trigger code was still accepting 200 records at a time. Also, static variables within a class were preserved in Bulk API mode, despite this documentation. I suspect that while this may have been true in the past, this behavior is no longer true, and the documentation was simply never updated to reflect this change in behavior. That said, however, you should never trust any static variables to exist across non-recursive trigger contexts (e.g. it's okay to use a variable to prevent recursion, but not to track state between triggers); sufficiently large Bulk API requests, for example, may lose the static variable data at any point during the transaction.

1
  • thanks for response..but what exactly due to mean by 'track state between triggers'? or 'you should never trust any static variables to exist across non-recursive trigger contexts' can you provide a very basic scenario
    – sfdc99999
    Jul 28, 2014 at 17:36
0

Salesforce uses interfaces to process any Apex. This means that Apex Classes are loaded every instance of a request. Using your Trigger example, whenever the Trigger is called, such as when using Bulk API, Salesforce will load a "fresh" MyClassForTrigger. The interesting part is that the default setting for Bulk API is to batch requests into batches of 200 records. That means that when Salesforce is processing the request, Salesforce loads any Apex Classes, such as MyClassForTrigger, every batch effectively "resetting" any static variables between batches. Any Apex is not saved in a "state" for the whole request -- each batch is ran independently (and most likely in parallel threads). Another way to phrase this is static variables are born and die in their execution context. And in the Bulk API example, each batch of the request has its own execution context.

When implementing Database.Batachable, one can also implement Database.Stateful so that instance variables are kept between batches.

1
  • The Bulk API doesn't behave this way, unfortunately. While the transactions are broken up into 200-record execution contexts, static variables retain their state. This makes certain triggers very difficult to support with the Bulk API. Jul 28, 2014 at 17:16

You must log in to answer this question.

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