Wednesday, October 10, 2018

Governor limits in salesforce

Why governor limits?

Governor limits are enforced to ensure that code runs correctly without any error. Governor limits ensured that you write correct code considering best practices.

Governor limits in salesforce

Limits value:

SOQL queries per transaction ==> 100(synchronous)

SOQL queries per transaction ==> 200(asynchronous)

Number of record retrieve by SOQL query ==> 50000.

Dml statements per transaction ==> 150

Number of records proceeds by dml statement ==> 10000

Let's us try to insert more than 10000 records using dml statement.

List<Account> accList= new List<Account>();
for(integer i = 1; i<20000 ; i++){
Account acc= new Account(); = 'Test' + '-' + i;
insert accList;

EXCEPTION: System.LimitException: Too many DML rows: 10001
STACKTRACE: AnonymousBlock: 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

Number of Apex jobs in queue ==> 50

Heap size limit:

==> 6 MB ( synchronous)
==> 12 MB ( asynchronous)

Note: Refer list for loop vs soql for Loop article for example on heap size error.

SOQL List vs SOQL For Loop

How to avoid hitting governor limits in test method ?

  • Using Test.startTest() method.

Points to remember to avoid hitting governor limits:

Salesforce Governor Limits

1) Avoid writing for loop inside the method.

2) Use soql for Loop instead of a list for Loop.

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.

TAGS: Governor limit in salesforce,Salesforce Governor Limits,how to handle governor limits in salesforce,how to overcome governor limits in salesforce,governor limit exception in salesforce.

No comments:

Post a Comment