What are Governor Limits in Salesforce?

What are Governor Limits in Salesforce?

Understanding Salesforce Governor Limits using Apex

ยท

4 min read

In this article, you will learn about Governor limits in Salesforce. Governor limit is one of the biggest challenges faced by salesforce developers. Salesforce throws a run time exception whenever limits are exceeded in the apex code.

What is the governor limit in Salesforce?

As a Salesforce developer, you already know that Salesforce is based on the concept of multi-tenant architecture which means the same set of resources is shared by multiple clients. Hence to avoid monopolization of shared resources Salesforce enforces some limits so that no one can overuse the resources. These limits are known as governor limits.

How to avoid hitting governor limits?

In order to understand how to avoid hitting governor limits we will look at some well-known limits such as the number of SOQL & DML allowed in a single transaction. Let us understand both of them using Apex code

System.LimitException: Too many SOQL queries

Run the below Apex code from the anonymous window.

for(Integer i=0;i<101;i++)
{
    List<Account> accList = [Select id from Account];
}

You will get the following exception.

Also when you open the logs and scroll down to the bottom You will see the following messages.

The error which we got is System.LimitException: Too many SOQL queries: 101. Now the reason for the exception is that Apex allows 100 SOQL queries in a single apex transaction but here we have executed SOQL queries 101 times using for loop and hence got a limit exception.

So now let's try the below code.

for(Integer i=0;i<100;i++)
{
    List<Account> accList = [Select id from Account];
}

This time you will not get any exception but still, the code which we have written is not following the Apex Best practices. One of the best practices is to never write any SOQL query inside for loop because there might be a chance of hitting the governor limit which is what we did in the first example code.

System.LimitException: Too many DML Statements

Run the below apex code from the anonymous window.

for(Integer i=0;i<151;i++)
    {
        Account acc = new Account(Name='Some Random Account '+i);
        insert acc;
    }

You will see the following exception.

Also when you open the logs and scroll down to the bottom You will see the following messages.

The error which we got is System.LimitException: Too many DML Statements: 151. Now the reason for the exception is that Apex allows only 150 DML operations in a single transaction but here we have made 151 insert operations which are more than the allowed limit and hence we got a LImitException.

Now let's try the following code.

List<Account> accList = new List<Account>();
    for(Integer i=0;i<151;i++)
    {
        Account acc = new Account(Name='Some Random Account '+i);
        accList.add(acc);
    }
    insert accList;

This time you will not get an error and all 151 account records will be created. What we did is created a list of accounts and added all the account records on that list. And run one single insert operation on that list after the for a loop.

Hence here also we are now following Apex's best practices.

There are different types of governor limits in Salesforce. But in this article, we have looked at two of the most common limit which we see while writing apex code. You can learn about other types of limits from the Apex Developer Guide.

If you are facing difficulty understanding the governor limit then watch the below video.

What are governor limits in Salesforce?

Note: Also keep in mind that we will not be able to handle LimitException using a try-catch block in Apex Code.

Conclusion

Governor limits are enforced by salesforce so that no one can overuse shared resources. In order to avoid hitting the governor limit in apex code always write the code using best practices defined by salesforce. We have already looked at some of the best practices in the above examples. I will write an article on Apex's Best Practices in the future.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here. Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย