3

Can anyone help me in understanding the significance of having a Static variable in Trigger with any real time example. For Example

Trigger on Account(after Insert,after Update)
{
   Static String testVariable = 'Hello' // Why do we declare Static in a trigger what is the significance? 
}

I understand the significance of it in a class. A static variable defined in a class serves as a common variable which is shared across all the objects of the class.

1
  • 1
    @BrianMansfield that doesn't work if the variable is in a trigger, the static variable would have to be in a class.
    – martin
    Jun 13, 2017 at 14:17

3 Answers 3

2

static in a trigger is an allowed keyword, most likely as a way of simplifying the compiler logic, but has no effect when used in a trigger. Static variables are not persisted in a trigger, only in classes.

2

Here I got a thread on Stack Exchange that states the same query. I suppose that will be nice if you go through this one:

Static variables defined in a trigger

3
  • Thanks for your answer; I've flagged this question as a possible duplicate.
    – sfdcfox
    Jun 13, 2017 at 14:26
  • 1
    That was one elaborate discussion in that thread. Feels good that I was able to help through! Jun 13, 2017 at 14:33
  • 1
    Please do not solicit voting behavior in your answers.
    – Adrian Larson
    Jun 13, 2017 at 22:59
1

Static variables in triggers are allowed by the syntax of Apex, but in my 4.5 years on the Salesforce platform, I have not found a practical use for them.

The reason is that static variables defined in a trigger behave differently than static variables defined in an Apex class do. It's more accurate to say that Triggers behave differently than regular Apex Classes

The documentation page Static and Instance Method, Variables, and Initialization Code covers the differences between static vars in classes, and static vars in triggers pretty well.

Probably the most important bit in regards to your question here is the following paragraph from that page:

A static variable defined in a trigger doesn’t retain its value between different trigger contexts within the same transaction, such as between before insert and after insert invocations. Instead, define the static variables in a class so that the trigger can access these class member variables and check their static values.

Going further, a static variable defined in a trigger (because we can't currently define a static{ ... } block inside a trigger) is re-declared and re-initialized each time the trigger is executed. Combine that with the fact that Salesforce breaks trigger executions into batches of (at most) 200 records, and the result is that variables declared static in a trigger, in addition to not being retained between different trigger events like before insert and after insert, are not retained between separate trigger batches if you're performing DML on more than 200 records of a single object at once.

You can verify this for yourself by adding the following snippet to any trigger

// I assume this is either an insert or update trigger
Static List<SObject> encountered;

// If static vars in triggers are persisted across contexts, this if would
//   prevent us from overwriting our existing list with a new (empty) one.
if(encountered == null){
    encountered = new List<SObject>();
}

encountered.addAll(trigger.new);

system.debug('Number of records in trigger static variable: ' + encountered.size());

And then looking at the execution log after executing the following as anonymous apex

// In this example, I added the trigger snippet to a Contract trigger.
// You'll need to adjust this to create appropriate records for whatever trigger
//   you added the snippet to.
// Also, probably a good idea to do this in a sandbox environment (as opposed to production)
Account acct = [SELECT Id FROM Account WHERE RecordType.Name = 'Billing' LIMIT 1];
List<Contract> contracts = new List<Contract>();
for(Integer i = 0; i < 210; i++){
    contracts.add(new Contract(
        AccountId = acct.Id
    ));
}

insert contracts;

// In your debug log, you should see 
// 'Number of records in trigger static variable: 200'
// and then later on...
// 'Number of records in trigger static variable: 10'
// These may be repeated, depending on if your trigger handles both the 'before' and
//   'after' events, but it will never result in seeing '210' (or anything
//   greater than 200)

So the end result is that static variables defined inside of a trigger are useless.

2
  • 1
    Assuming static values were persisted through trigger contexts, your code example is incorrect; it would be written like this: Static List<SObject> encountered; if(encountered == null) { encountered = new List<SObject>(); } encountered.addAll(trigger.new); But, as you said, it's not supported.
    – sfdcfox
    Jun 13, 2017 at 14:23
  • Ah, indeed. Guess my brain isn't quite up to speed yet this morning. Editing my answer.
    – Derek F
    Jun 13, 2017 at 14:27

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