Quantcast
Channel: Veon Consulting
Viewing all articles
Browse latest Browse all 166

Salesforce Governors Limit | Understand with examples

$
0
0

Just like any SaaS based application, Salesforce has Governor Limits which restrict the excess usage of shared resources on the cloud, thereby enabling Salesforce developers  to write efficient code. This article helps the readers to understand what they rules are. Therefore you can write efficient APEX codes and meet your goals by using application in an optimized manner.


What are the Governors Limits

Following are the common Governor limits which every developer must be aware of. This will help solve unwanted issues and lags within the system. They are classified under various sections like APEX, Salesforce Web-service calls, SOQL and SOSL so that you can narrow down on your interest areas.

  Maximum push notification from APEX transaction

Group Description of Group Limit Parameter

(This section describes the parameter on which limit exists)

Limit Value
SOQL If you are aware of how we use SQL queries within applications, SOQL is exact equivalent of the same within Salesforce. Maximum number of rows you can fetch in a SOQL

Maximum number of queries you can initiate

50,000

100 (Synchronous)

200 (Asynchronous)

 SOSL  If you are performing text searches across objects this translates to SOSL queries within Salesforce. Maximum number of records you can fetch in a SOSL query

Limit on number of SOSL query you can issue

 2,000

20

 DML Data manipulation language operation to perform CRUD operation in a mass manner Total number of DML statements issues

Maximum number of records which can be processed on DML operations

150

 

10,000

 HTTP requests These are web service calls which can be initiated from other applications to Salesforce. This is crucial if you are looking at integration solutions within the application. Total number of call-outs in a transaction

Total cumulative timeout on web-service call in a transaction

100

120 seconds

 APEX  If you have custom logic developed, this would be done in APEX programming language. There are certain limits which apply to APEX developments Maximum processing time for an APEX transaction

Maximum push notification from APEX transaction

10 minutes

10

Now, we will be discussing about how to optimize the developments so that we never hit these limits.  Some Governor’s limits are based on the transactions. A transaction is a set of actions that can be executed as a single unit. These could be class or triggers from Salesforce development perspective. For simplicity sake we will see how to work around the following limits

#1 Limit on DML operations per transaction

#2 Limit on SOQL  per transaction

#3 Limit on number of records which can be processed within a DML operation

 

Going through these examples, you may be able to find solutions and best practices around other limits as well.


Account Sync SAP Business One and Salesforce Integration

Related:-  SAP Business one and Salesforce Account Sync


1. Maximum Number of DML Operations per transaction is 150

DML stands for ‘Data Manipulation Language’ which modifies the data using five operations. The 5 DML operations are insert, update, delete, merge and restore. Data in Salesforce can be managed using these 5 operations.

The number of DML operations allowed per a transaction is 150. So, the code should be written in such a way that this limit should not be reached. Below example shows the code with 150+ DML operations.

DML Limit code

  • In the above screenshot, insert is called thrice in line numbers 5, 9 and 13.
  • In line no 5, insert will be executed once.
  • In line number 9, insert is in for loop which is repeating for 150 times. So, insert operation will be executed 150 times.
  • In line number 13, insert is executed once.
  • Total number of times insert will be executed is 152

Now,  execute the class by navigating to Debug ->’Open Execute Anonymous Window’

Enter the following code and click on ‘Execute’ button

DMLLimit dl=new DMLLimit();
dl.test();

Now, the following screen will be shown which says ‘Too Many DML statements’

DML Execution Error

Here we’re writing ‘DML statement’ (insert) inside the ‘for loop’. So, it’s executing 150 times.

This can be avoided by using ‘List’ in the above class. Similar operations of an object can be added to a list and perform DML operation on the list. In this way, the above code can be executed without hitting ‘Governors Limit’. Below is the modified code which doesn’t hit governor limits.

Public class DMLLimit{
Public void test(){
	List<Account> acclist=new List<Account>();
	Account a= new Account(Name='test56');
	acclist.add(a);
	for(integer i=1; i<150;i++){
		Account acc=new Account();
		acc.Name='test5'+i;
		acclist.add(acc);
	}
   Account a1=new Account();
	a1.Name='test56';
	acclist.add(a1);
	insert acclist;
}
}

  • In the above code, a list is created in line no 3 which holds ‘Account’ data.
  • In line no 5, account ‘a’ is added to list ‘acclist’.
  • In line no 9, account  ‘acc’ is added to the same list and insert operation is not performed in the loop.
  • In line no 13, account ‘a1’ is added to the list ‘acclist’.
  • Insert operation is performed in line no 14 on the ‘acclist’. As all the accounts are added to ‘acclist’, all records can be inserted using the single insert operation on the list ‘acclist’.

In this way, we can avoid reaching ‘Governor limit’ while using DML statements in a transaction.


2. Maximum Number of SOQL Queries per transaction is 100

Salesforce provides ‘Salesforce Object Query Language’ to access the records existing in Salesforce. ‘SOQL’ queries are executed in ‘APEX’ to retrieve data from Salesforce. Salesforce allows only a maximum of 100 SOQL queries per transaction. So, the code should be written in such a way that this limit should not be reached. Below example shows the code with 100+ SOQL statements.

public class SOQLLimit {
public void test(){
    List<Account>  acclist=[select Id, Name from Account];
    for(Account a:acclist){
      List <Contact> cont=[select Id, LastName from Contact where AccountId =: a.Id];
        for(contact c: cont){
            c.LastName='test';
        }
        update cont;
    }    
}   
}

In the above example, ‘SOQL’ query is written within for loop. So, ‘SOQL’ query executes for every ‘for loop’ iteration. An account can have 1 or more than 1 contact. If there are 100 accounts, the probability of the number of contacts is greater than or equal to 100. In this case, Governor limits will be reached and class fails to execute. Below screenshot shows the error for the same.

SOQL limit Execution Error

Following screenshot shows the correct code written without hitting Governors limit. Here, ‘SOQL’ query is written outside the ‘for loop’ which retrieves the contact details of Account and operations on performed on the list.

public class SOQLLimit {
	public void test(){
        List<Account>  acclist=[select Id, Name from Account];
        List <Contact> cont=[select Id, LastName from Contact where AccountId In:acclist];
         for(contact c: cont)
         {
                c.LastName='test';
         }
         update cont;
       
	}   
}


3.Maximum Number of records in a transaction on which DML operations can be performed is 10,000

The below example shows inserting records to ‘Account’ object. A list is created to store the records which need to be inserted.

  • The  First record is added to list in line no5.
  • The Second record is added to list in 7.
  • As the list is in ‘for loop’, 10,000 records will be added to list in line no 11.
  • The total number of records to be processes 10002.

public class SOQLLimit {
	public void test(){
        List<Account> accounts=new List<Account>();
       	Account acc=new Account(Name='Test');
        accounts.add(acc);
        Account acc1=new Account(Name='Test');
        accounts.add(acc1);
        for(integer i=0;i<10000;i++)
        {
            Account acc3=new Account(Name='Test');
            accounts.add(acc3);
        }
        insert accounts;
	}   
}

When above class is executed, Salesforce throws the error as shown below. This can be avoided by using ‘Batch Apex’.

Maximum Number of records in a transaction Execution Error

These are the most common Governor limits faced by every developer. Apart from this, there are few other limitations imposed by Salesforce.  All the Governor limits imposed by Salesforce can be seen from the developer console by following the steps given below.

Open developer console -> Navigate to Debug ->’Open Execute Anonymous Window’ -> Enter ‘system.debug(‘test’);’  and execute this.

Following Governor limit screen will be shown.

list of Governor limit

From the above scenarios we can find that Governor Limits are reached by writing SOQL and DML statements in ‘for loops’. We can prevent this by following above principles.


We hope that this tutorial is genuinely useful for all Salesforce developers. If you have any comments or feedback, we would love to hear from you on that.


Viewing all articles
Browse latest Browse all 166

Trending Articles