Salesforce Governor Limits and How to Handle Them

Salesforce makes cloud-based software that helps businesses to run their Sales, Marketing and Service operations. The Salesforce platform has been used by many organisations around the globe to run their IT and business operations in the cloud.

By combining sales, marketing, and services Salesforce enables organizations to effectively manage customer data. The platform comes with an array of tools for designing, building and customizing applications and solutions according to the needs of the business. Salesforce has been very successful in the enterprise software space with its multitenant architecture and continuous to dominate in the SaaS market.

Multi-tenant Architecture

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 apartments 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 multitenancy architecture.

Multitenant architecture does have some limitations as the database is being shared with other tenants in the Salesforce Platform. To ensure the platform is efficient for all tenants, Salesforce has a set of Governor limits that the customers will have to factor in while designing solutions on the platform. For example, the platform scans the code script and limits how much CPU time it can use, how much memory it can consume, and the number of DML statements it can execute. 

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

• Per-Transaction Apex Limits – These limits count for each Apex transaction. For Batch Apex, these limits reset for each execution of a batch of records in the execute method.

Per Transaction Certified Managed Package Limits – If a managed package developed by a Salesforce ISV has passed security review, they are provided with generally higher per-transaction limits.

Lightning Platform Apex Limit – These limits aren’t specific to an Apex Transaction and are enforced by the Lightning platform.

Static Apex Limit – Apex Limits that are applied across all transactions.

• Size-Specific Apex Limit – Apex Limits related to the size of the code.

• Miscellaneous Apex Limit – …other Limits!

See the documentation here.

Best Practices around Salesforce Governor Limits

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.

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. Developers and Architects should adopt best practices and patterns while building solutions in the Salesforce Platform which would help the organisations to stay within the allowed threshold by Salesforce. 

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.

Want to learn more? Let's talk: