i n o v i
Published Date: February 8, 2023 Nazim Khan

How to Handle Governor Limits in Salesforce

Has your business come to a standstill because Governor limits are breaking your code down in Salesforce? Well, you are not alone. Governor Limits might be a nuisance, but they serve an important role.

Blog Image

Read on to learn how to handle Governor limits in Salesforce using a few best practices and regain control of your business.

What is Salesforce Multitenancy?

Salesforce operates in software multitenancy, which means a single software runs on a server that serves multiple users. Since you are essentially sharing resources in multitenancy, governor limits are used so that any Apex* you develop does not monopolize the resources available.

*Apex includes an API (application programming interface) that Salesforce developers use to access user data on the platform.

Think of Salesforce multitenancy as renting a flat in an apartment building. You and the other tenants share common water, gas, electricity, and wi-fi resources.

Imagine if several flats start mining bitcoin, downloading large 4K videos, or running a bath simultaneously. This would lead to a nightmare scenario where you can’t access any of your shared resources adequately.

In this analogy, the apartment building is the Salesforce server, the flats are individual Salesforce orgs, and the distribution of resources is the multitenancy structure. This structure prevents inconsiderate users from overusing the database, memory, processor resources, or network you and others on the same server will need to run their businesses effectively.

Governor limits determine what your code can/can’t do when used on the Salesforce platform since the resources used are defined.

What are Salesforce Governor Limits?

In simple terms, Salesforce Governor Limits are a set of usage rules that Salesforce enforces to ensure processing efficiency. This allows multiple users without the performance suffering.

There are multiple types of governor limits, but it usually depends on the edition of Salesforce you are using. The professional edition lets you have 50 custom objects, whereas unlimited and performance editions can have 2,000.

Some soft limits can be resolved either by Salesforce themselves or by buying an add-on. Other limits include a combination of limits set by your Salesforce edition and the number of user licenses on the org.

Types of Salesforce Governor Limits?

There are six main types of governor limits that Salesforce enforces on its servers:

  • Per Transaction Apex Limits: These are for every Apex transaction. They are reset after every execution of a batch of records for batch Apex.
  • Per Transaction Certified Managed Package Limits: If a managed packed Salesforce ISV has developed that has passed the necessary security reviews, they are given larger per-transaction limits.
  • Lightning Platform Apex Limit: Not specific to any particular Apex, and the lightning platform enforces it.
  • Static Apex Limit: These limits are applicable for all transactions.
  • Size-Specific Apex Limit: Determine how big or small the code is.
  • Miscellaneous Apex Limits: All other limits that are used.

Why are Governor Limits Important?

Governor limits are a must-know concept for Salesforce developers. Governor limits determine the architecture of your Salesforce solution and how you write the code. If you hit a Salesforce Governor limit, a runtime exception will be issued to break down your code and disable it.

Best Practices to Handle Governor Limits

Governor limits are there for a reason and are something you cannot avoid being a part of. However, you can use some best practices that will help you avoid being hindered by these limits.

Some of these best practices are as follows:

One Trigger Per Object

A single Apex Trigger is enough for a particular object. If you run multiple triggers for a single object, you can’t control the execution order if the triggers run in the same context.

Logic-less Triggers

If you add methods to your triggers, you can’t expose them for testing. Neither can you expose the logic for re-use elsewhere in your organization.

Context-Specific Handler Methods

Use context-specific handler methods from Trigger handlers.

Add Bulk to your Code

Adding more bulk to your Apex code means that your code can handle more than one record simultaneously.

Avoid using SOQL Queries or DML statements inside FOR Loops

You can get a maximum of 100 SOQL queries with a single Apex request before exceeding the governor limit. Hence, operating a batch of less than 100 account records is okay. Otherwise, the governor limit will put a runtime exception.

Using Collections, Streamlining Queries and Efficient FOR Loops

Using Apex Collections, you can query data and store it in memory. Using a combination of collections and streamlining SOQL queries can help you write efficient Apex code and avoid governor limits.

Querying Large Data Sets

You can return 50,000 records per SOQL query. If you exceed your limit, use a SOQL query for loop instead. It can process multiple batches of records using internal calls to query and query More.

Avoid Hardcoding IDs

While deploying Apex code between sandbox and production environments or installing Force.com AppExchange packages, you mustn’t hardcode IDs into the code. Even if the record IDs change between settings, the logic can dynamically find the correct data to operate against.

Conclusion

There are many different types of governor limits across Salesforce. These are meant to increase overall efficiency for all users, but they also help developers and admins be more efficient. You can use some best practices, as discussed in this article.

Hopefully, you can use the knowledge you have gained from here to improve your processes and be better capable of handling Governor limits in Salesforce.

FAQ

How should you deal with the governor limits in Salesforce?

  • Exclude DML statements or SOQL queries in our FOR loop.
  • Limit using SOQL or DML operations in the loop.
  • Try to bulkify the code and helper methods.
  • Query large data sets.
  • Use Batch Apex to process 50,000 records.
  • Streamline various triggers on the same object.

How do I monitor governor limits in Salesforce?

You can see limits by going to Setup->Company Information

What happens if an operation in code exceeds a governor limit?

If some code exceeds the limit, the associated governor issues a runtime exception that can’t be handled.

What are the different types of governor limits in Salesforce?

  • Per-Transaction Apex Limits
  • Per Transaction Certified Managed Package Limits
  • Lightning Platform Apex Limit
  • Static Apex Limit
  • Size-Specific Apex Limit
  • Miscellaneous Apex Limit

Why are there governor limits in Salesforce?

Salesforce Governor Limits are some set of usage rules enforced by Salesforce to ensure efficient processing. These allow for multiple users to use the platform without impeding performance.