Salesforce Apex Performance Best Practices | Apex

There are bunch of articles out there talking about best practices in Salesforce. I see some of the common practices that every ...

There are bunch of articles out there talking about best practices in Salesforce. I see some of the common practices that every salesforce developer should be aware of, I developed notes over period of time with some examples to follow.  I added references to bunch of key sources and articles down at the bottom covering some official articles and as well as good StackExchange articles where people have done amazing job responding to questions.

Querying Salesforce in Trigger : 

Make sure you have no SOQL in for loop

Wrong Approach

Recommended Approach

Bulkify Trigger with Set and Map: 

Salesforce provides standard data-structures like Maps and Set to per-filter and pre-populate data to avoid repeated SOQL calls, this allows user to handle bulk data, often called bulkification of code. Triggers in salesforce are intentionally designed to handle bulk data, so here we simple pull Ids into a set which is used to iterate and populate a Map holding all values. The map is our data storage and used for querying Salesforce

Recommended Approach

SOQL In for loop for large datasets : 

SOQL-IN for loops, allow you to set the range of for loops through a SOQL query, example below shows two ways of writing for-loops. First approach may throw error of 'heap size' governor limit.

Avoiding SOQL Injection :

SOQL injection, allows user to pass set of keywords input, which becomes wildcard to access all data in Salesforce, if query is pro-actively designed to handle those wild card. A simple example below demonstrates SOQL injection in action.

Vulnerable to SOQL Injection

Dynamic SOQL

Querying Child Items :

Inefficient way of querying child item would iterating through each parent object and querying child item, which is ugly. Smartly this can be done as show below.

Wrong Approach

Recommended Approach

@future method in Salesforce : Making integration/service communication future proof.

Annotate a method with future, when you want to perform a job asynchronously, like making an outbound call to Facebook, Twitter on a field update.Ideally if it is not 'asynchronous', then salesforce hang up and wait for response and this delay or hang up process. But key is, writing logic of @future method in separate class.

Recommended Approach : Write your @future method and logic in separate class

Call Asynchronous class through Apex Trigger

Things to remember

  • Future cannot call another future method neither a batch job can call @future method
  • Use callout=true for making callouts
  • You cannot guarantee order of execution
  • Future call have limits and can run concurrently
  • Check video, below if you want to get-around these limits in some ways 
  • Video covers, future calling future and chaining asynchronous calls

Better {get; set;} method using pattern called 'Lazy Loadin

Try to  add filter if possible in your get property to only query salesforce conditionally, when some criteria is met, and avoiding querying salesforce every time the class constructor called.

Recommended Approach : Adding conditional logic in getter

Transient Keyword and Properties to reduce view state

Transient keyword is used for properties or variable, which don't want to store on the view part of the application, this can be used primarily to reduce down the view state of visualforce page,  you should remember that static method are by default transient and simple example to understand usage of transient is shown here and then another awesome discussion in here on stackexchange, which talk about another side of visualforce, when your UI has many components and result in heavy view state

I strongly recommend reading this article mentioning some read best practices for improving visualforce performances

How to make Salesforce Properties Transient : Check out similar blog post here for explanation in detail

Best Practices for View State 

Best Practices for Asynchronous Apex 

Read More :

Design Patterns by +Andrew Fawcett :

 Other Resource for Best Practices

What Others Are Reading