Salesforce Governor Limits and How to Handle Them

Quality Clouds > Salesforce  > Salesforce Governor Limits and How to Handle Them

Salesforce Governor Limits and How to Handle Them

The Salesforce platform sees considerable usage by organizations all around the world. For more than two decades now, Salesforce has been involved in effective management of customer data with many of its groundbreaking concepts and policies such as multitenancy and ability to access your data anywhere at any time. To achieve this Salesforce Developers are equipped with an array of tools for designing, building and customizing several Salesforce features, platforms and applications according to the needs of the business.

However, the development and operations of Salesforce apps is not without restrictions and regulations. The majority of these regulations comprise of Governor Limits.

However, in order to understand what these Governor Limits are and how they restrict Salesforce developers, let us first understand the concept of a multitenant architecture.

 

Salesforce Multitenancy

 

Salesforce multitenancy is very similar to renting a flat in an apartment building. You share common resources such as water, electricity, gas and wi-fi bandwidth. Now imagine if several flats started plugging in heavy duty appliances that could consume all of the electricity. Then imagine if some other flats started to download huge files from the internet. And then also imagine if some other flats had running water all at the same time. It would be difficult to live in a flat where you wouldn’t be able to get proper electricity, running water or a stable wifi bandwidth even after paying a premium.

The apartment building is a Salesforce server and the flats are the individual Salesforce orgs. The distribution of shared resources is the multi-tenancy architecture.

This analogy is useful in explaining why governor limits are so helpful in preventing developers from accidentally hogging database, memory, network or processor resources that other customers on the same server need to effectively run their business processes in the cloud. So, governor limits regulate what your code can do when it is run on the Salesforce platform. And this in turn helps the scalability of the platform since the resources are predefined. Quality Clouds helps customers understand their Governor Limit usage and take corrective actions to optimize their Salesforce orgs.

 

Types of Governor Limits

 

 

Best Practices around Salesforce Governor Limits

As much as it is important for developers to know all the relevant Governor Limits, it is also important for them to know how to handle Governor Limits in Salesforce. There are certain best practices you can follow in order to avoid these limits, some of which are mentioned here:

 

One Trigger Per Object

 

A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

 

Logic-less Triggers

 

If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic for re-use anywhere else in your org.

 

Context-Specific Handler Methods

 

Create context-specific handler methods in Trigger handlers

 

Bulkify your Code

 

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

 

Avoid SOQL Queries or DML statements inside FOR Loops

 

An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

 

Using Collections, Streamlining Queries, and Efficient For Loops

 

It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

 

Querying Large Data Sets

 

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

 

Avoid Hardcoding IDs

 

When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

 

Conclusion

In conclusion, there are many different types of governor limits across Salesforce that help us become more efficient developers and admins. There are some best practices that you can adopt to ensure that you stay below the limits. The most popular limits are those around SOQL and DML limits in a single transaction. Quality Clouds tracks these best practices around governor limits in your orgs and can help you identify violations. If you would like to discuss how to implement Salesforce Coding Best Practices in your SDLC get in touch with us today.

No Comments

Post a Comment

Comment
Name
Email
Company