Ever since its launch, Salesforce CRM has become the most popular and powerful platform in the tech industry. It is one of the first cloud computing companies which deliver its product and services through an internet browser and not servers sitting in your company’s office. 

In this regard, Salesforce is considered an innovator. And due to its immense popularity, it is used by a number of businesses and enterprises worldwide. Salesforce has a vast number of servers across the world and these servers have huge databases and machines. These powerful servers can host many instances of Salesforce from different companies.

Salesforce resources are finite and are shared by organizations and users worldwide. To restrict the monopolization of these resources and to ensure they are being used in a fair manner, the company has established certain rules and regulations. Governor Limits are one such set of rules introduced by Salesforce. 

The Salesforce developers cater to their client’s requirements and offer multiple services in development and design. The developers must comply with the regulations while using the Salesforce CRM platform.

What is Salesforce Multitenancy?  

Salesforce runs on a multitenant form. A classic example to understand this would be that of a building that has flats shared by different owners. The resources like electricity, gas, and wi-fi are all shared by them. 

Imagine if numerous flats start downloading huge 4k videos, plug in heavy-duty electric equipment, or continue running water for long hours, what would happen? Your electricity bill would go up, you will not receive proper wi-fi bandwidth and the water pressure in your bathroom will decline. In short, it will be a nightmare to live in such a place.

The flats represent separate Salesforce organizations, and the building represents a Salesforce server. The shared resources form the multi-tenancy architecture. And now you get a clear picture of why Salesforce enforces regulations on using its resources. It prevents users from overusing resources like networks, databases, memory, processor resources, etc so that an organization can run its business in an efficient manner. 

What are Salesforce Governor Limits?

Governor Limits in Salesforce, also known as Apex Governor Limits are runtime limits imposed by the apex runtime engine to ensure code scalability and efficiency. Because apex runs in a multi-tenant environment, these limits make sure that the resources are not monopolized.

In simple terms, Salesforce Governor limits are usage caps enforced by Salesforce for efficient processing. Governor Limits control on how much data can be stored in shared databases. If any of the Salesforce Governor limits gets exceeded, an error will be generated, and the users will be notified on time. Due to the error, the user’s program would shut down.

Types Of Governor Limits

  • Per-Transaction Apex Limits: These limits are counted for each Apex transaction. For Batch Apex, these governor limits are reset for each execution of a batch of records in the execute method.
  • Per Transaction Certified Managed Package Limits: These limits are enforced on managed packages and are different from those applied to Apex which is not a part of the managed package. If a managed package built by a Salesforce ISV has passed a security review, the per-transaction limits are generally higher.
  • Static Apex Limit: These limits are applied for the static apex. They are applied for different transactions undertaken by the users.
  • Size-Specific Apex Limit: These limits relate to the size of the code.
  • Lightning Platform Apex Limit: These limits are enforced by the Salesforce Lightning Platform. They do not apply to Apex transactions executed by developers.

Importance of Salesforce Governor Limits

Salesforce Governor Limits are important to make sure that the platform runs smoothly and that there are no server failures due to overuse or monopolization of its finite resources. The Salesforce developers run many customizations on the platform and if they hit the governor limit, the relating governor issues a runtime exception. The code fails and breaks. Salesforce governor limits ensure that the code is written efficiently and is not lengthy. 

Salesforce Governor Limits Best Practices

As a salesforce developer, we need to make sure that the code is scalable and does not exceed governor limits. There are certain apex best practices to follow to make sure we don’t hit these limits. Some of the popular limits are around SOQL and DML in a single transaction.

  • SOQL Limit 

There is System limit exception too many soql queries 101 governor limit set by Salesforce. The developers encounter this limit very often. Here’s what it looks like and how you can solve this problem: 

In the above example, the system will throw an error because the SOQL statement has been used inside a loop. It’s a big mistake and due to this, the code will fail. There is a synchronous limit of 100 and an asynchronous limit of 200 records when it comes to the total number of SOQL queries issued. 

An easy solution to this issue is to move all SOQL statements or queries outside the for loop. You can also use collections to avoid SOQL inside the loop or Bulkify apex trigger to evade recurring issues in your code. 

If there are more than 50,000 records then use Batch Apex. Also, it’s a good practice to streamline the execution of various triggers on a single object.

  • DML 150 Limit

The second most popular limit is the number of DML operations in a single transaction. You will get a “Too Many DML Statements: 151” error if you exceed the limit of 150 DML operations.

In the above example, just like the previous one, the system will throw an error because a DML statement ( insert accountObject 😉 has been used inside a for loop. To avoid making this mistake, developers need to use the DML operations outside the loop or use collections to store the data and perform DML on a collection. In this way, you could perform a DML operation on tens of thousands of records and it will be only counted as one DML. Yes! Isn’t that so efficient?

Final Thoughts

Governor Limits in Salesforce ensure code scalability and efficiency. As you can see, there are many governor limits enforced by Salesforce and two of the most well-known are around SOQL queries and DML operations. These help the admins and developers to work in accordance with the fact that Salesforce’s resources are not infinite or unlimited. 

The general apex best practices help in staying away from hitting governor limits. It is both a challenging and fun process. Hopefully, this article was helpful to you in understanding what governor limits are and the best practices to employ for getting past them!