# What are Governor Limits in Salesforce?

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.

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

You will get the following exception.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674286699920/9d650e8f-428f-4f70-b5d6-bca4fd3a1f1d.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674286870056/213eecf3-7f0f-4fa8-9859-fb51f8f6ef53.png align="center")

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.

```java
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.

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

You will see the following exception.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674288171477/51e86e26-4a05-48cf-929c-13282c00da2d.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674288258089/e2609519-edac-41bb-93aa-34cd08326624.png align="center")

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.

```java
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](https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm).

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

[![What are governor limits in Salesforce?](https://i3.ytimg.com/vi/Jdsk86XwOJM/maxresdefault.jpg align="left")](https://www.youtube.com/watch?v=Jdsk86XwOJM&list=PLPrnFIEBKtbYWWYJN-2FsjpSJCADnNM_3&index=1)

**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**](https://twitter.com/ShubhamLashkan). Let me also know which topic I should write about next.

Learning Salesforce then do checkout Salesforce Verse: [https://salesforceverse.com](https://salesforceverse.com)
