The Importance of the Execution Context

Execution Context

One of the fundamental pieces of the Salesforce coding puzzle that I needed when beginning to develop on the platform was understanding the Execution Context.

Key things to know about the execution context:

  • Static variables live throughout the duration of the EC
  • Each EC gets its own set of governor limits

One mistake I made, and continue to see over and over again, is the lack of understanding how variables live during the Execution Context, specifically static variables.  I have found this to be particular true when I see a query for the same record type several times during the same context.  When your number of query calls is limited during an execution, it is pivotal that you aren’t wasting them querying for data that you already have.  Today this isn’t as much of a big deal because you no longer have to query for a record type and can use a describe call instead.

Knowing when and how your governor limits work is also extremely important.  With Salesforce being a multi-tenanted infrastructure, you don’t want one organization taking down everyone else because there is an infinite loop somewhere: hence, limits.  I don’t look at limits as actual being limited in my coding prowess, but instead, a challenge to use as little as possible to get the task at hand done.  In programming, there are always multiple ways to do something, I just need to find a more efficient way to get where I’m going.

There are many more parts to the execution context, especially when it comes to order of operations and when to use a Before or After trigger.  The basic understanding on how variables live in the Execution Context as well as limits you need to pay attention can determine the course you take to develop a piece of code.  Understanding what is happening in the big picture can eliminate duplication in your code, which can sometimes rear its ugly head when coding in Salesforce