Wednesday, October 10, 2018

Governor limits in salesforce

Governor limits are enforced to ensure that code runs correctly without any error. It is necessary to enforce governor limit since apex runs in a multitenant environment, multitenant here simply means a single instance of software runs on a server and serves multiple users at a time. The Apex runtime engine strictly enforces limits to ensure efficient performance.

Some of the important limits are mentioned below.

SOQL queries per transaction  100(synchronous)

100 Select statement per apex transaction.


SOQL queries per transaction  200(asynchronous)

200 Select statement per apex transaction.


Number of record retrieve by SOQL query  50,000


DML statements per transaction  150


Number of records proceeds by DML statement  10000


Let's us try to insert more than 10,000 records using DML statement.


List<Account> accList= new List<Account>();

for(integer i = 1; i<20000 ; i++){

Account acc= new Account();

acc.name = 'Test' + '-' + i;

accList.add(acc);

}

insert accList;

EXCEPTION: System.LimitException: Too many DML rows: 10001


STACKTRACE: AnonymousBlock: line 7, column 1


LINE: 7 COLUMN: 1


Total number of SOSL query  20


Number of record retrieve by SOSL Statement  2000


Number of future method call per Apex transaction  50 (synchronous)


Number of future method call per Apex transaction  0 in batch and future contexts; 50 in queueable context


Number of Apex jobs in queue  50


Maximum CPU time on the Salesforce servers  10,000 milliseconds       


Maximum CPU time on the Salesforce servers → 60,000 milliseconds


Maximum execution time for each Apex transaction → 10 mins for both synchronous and asynchronous


Total number of callouts (HTTP requests or web services calls) in a transaction  100 for both synchronous and asynchronous.


Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction → 120 seconds for both synchronous and asynchronous.


Heap size limit  6 MB ( synchronous)

Heap size limit  12 MB ( asynchronous)

Note: Refer below article for more details on heap size error and how to avoid it.

SOQL List vs SOQL For Loop


Points to remember to avoid hitting governor limits:

1) Avoid writing for loop inside the method.

2) Use SOQL For Loop instead of a SOQL List For Loop to avoid heap size limit error.


3) Use Test.startTest() method inside test method to reset governor limits.

 
4) Do not update setup objects like user role with other objects in the same transaction.

No comments:

Post a Comment