Blog

Governor Limits in Salesforce

  • (4.0)
  • | 2346 Ratings
Governor Limits in Salesforce

What is the Multitenant Architecture?

Salesforce is considered as a Multitenant architecture, where all users share the same infrastructure and the same version of the Salesforce platform. In comparing to their single-tenant platforms such as CSE(client-server enterprise) applications or mail servers, multi-tenant architectures will release upgrades automatically and simultaneously for all users. Therefore, there will be no worrying about maintaining their hardware or software, and we can make sure that their applications have the latest patches installed.

Governor Limits

This is to ensure we follow Salesforce standards. As Salesforce works in shared server format, means multitenant architecture, Salesforce wants the resources should not be over utilised by any client. Your code should execute within the execution limits which are predefined. If the governor limit exceeds, then the run-time error is thrown. By following the below mentioned best practices in our code, we can avoid these limit errors.

Few important Governor Limits in Salesforce

  • In one transaction, we can use a maximum of 100 SOQL queries.
  • In one transaction, we can use a maximum of 20 SOSL queries.
  • In one transaction, we can use a maximum of 150 DML statements.
  • In one transaction, we can fetch a maximum of 50k records in SOQL.
  • In one transaction, we can fetch a maximum of 2k records in SOSL.

Check Out Salesforce Tutorials

Description Synchronous Limit Asynchronous Limit
Number of queries issued by SOQL 100 200
Total number of records retrieved by SOQL queries 50,000  
Total number of records retrieved by Database.getQueryLocator 10,000  
Total number of SOSL queries issued 20  
Total number of records retrieved by a single SOSL query 2000  
Total number of DML statements issued 150  
Total number of records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin 10,000  
Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements 16  
Total number of callouts (HTTP requests or Web services calls) in a transaction 100  
Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction 120 seconds  
Maximum number of methods with the future annotation allowed per Apex invocation 50  
Maximum number of Apex jobs added to the queue with System.enqueueJob 50  
Total number of send email methods allowed 10  
Total heap size 6MB 12MB
CPU time on the Salesforce servers 10,000 milliseconds 60,000 milliseconds
Maximum execution time for each Apex transaction 10 minutes  
Maximum number of push notification method calls allowed per Apex transaction 10  
Maximum number of push notifications that can be sent in each push notification method call 2,000  

What are the ideal ways to ensure that our code won’t hit Governor limits?

  • Write code with collections so that for every record, you don’t need to write a separate query or separate DML.
  • Reduce the count of queries and try to fetch all needed data in the same query than calling again and again to Database.
  • Never write DML or query in any loop.
  • Use @Future annotation to bypass Governor limit errors.

Bulkification of Code:

Bulkification means, the code must work on one record as well as on many records. It means the code needs to be written in such a way that, if one record causes the DML or 100’s of records causes the DML, the code should handle all the records. Just by writing code suitable for multiple records is not going to give a proper solution. We need to focus and make sure that our code is not breaking the limits, i.e., Governor limits.

Governor limits + Bulkification = Ideal way of coding in Salesforce.

Frequently Asked Salesforce Interview Questions & Answers

Sample Program for Governor Limits & Bulkification: Using StartTest() or StopTest().

@isTest // Called as Annotation
Public class JeanDiscountClassTest(

	//we are calling MyTestFunction1(){} -------> 30 DML
	
	//we are calling MyTestFunction2(){} -------> 40 DML
	
Static testmethod void MyTestFunction3(){

	
	======some other code======
	======some other code======
	======some other code======

	Test.StartTest();

	//create a new record data

	Levis__c j = new Levis__c();
	j.Name = ‘Louis’;
	j.Price__c = 200;
	
	//insert jean
	Insert j;

//trigger will come to picture

//retrive the new jean
Levis__c j2 = new Levis__c ();

//same like, list<> MyList = New List<string>();
j2=[SELECT Price__c FROM Levis__c WHERE id=j.id];

//test that the trigger correctly updated the price
system.assertEquals(900, j2.Price__c);

	Test.StopTest(); 
}

======some other code======
	======some other code======
	======some other code======

	//we are calling MyTestFunction4(){} ---------> 60 DML
	
	//we are calling MyTestFunction5(){} ---------> 10 DML

)

In the next topic, we will discuss in detail about “Bulkification in Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close