Governor Limits in Salesforce

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9  ** Article 10

Salesforce being on cloud apex runs in a multi tenant environment. Because of this, the run time engine enforces  number of limits so as to make sure the running apex code does not monopolize the shared resources. Thus, when developing apex codes developers needs to be very careful, as a small mistake can lead to a Governor limit run time exception. Code should be written in such a manner that all of the specified governor limits in salesforce are taken into account.

Governor Limits in Salesforce

It is very important to keep in mind various limits that are in place. Some of the very important limits are described below with a situation where we can hit the limit and how we can avoid that scenario.

1. Total SOQL Queries that can be fired per transaction - The limit is 100 Queries.

We should make sure that we use minimum number of queries while writing apex code. Try to use logic that requires minimum SOQL. No queries within for loop should be used.
As an example,

for(account acc acclist){
    List<contact> conList = [select id from contact where accountid = acc.id]

}

In the above example there is a huge possibility that you will be hitting the 100 soql governers limit as soql is within for loop and will be executed the size of the acclist times and will hit the limit on 101 iteration. Hence we should never ever put SOQL within for loop unless we are 100% certain that the for loop will be executed only once. Collections like map,set,list should be used for building logic instead of putting queries in for loop

2. Total number of DML issued - limit is 150 DMLs

This means we can have only 150 dmls in one apex transaction. This can be avoided by writing the logic such that the dml's are done on collection instead of a single object records. Similar to no SOQL within for loop, No DML should be used within for loop. An example where we would hit the limit is as below,

for(account acc acclist){
     acc.name = 'test1'

     update acc
}

In above code DML is executed within for loop and hence will hit the limit once for loop iterate beyond 150. This can be avoided by doing DML on the entire list instead.

for(account acc acclist){
     acc.name = 'test1'

}
update acclist


3. Total number of records returned by SOQL query - limit is 50000

This means a soql queries will return back upto 50000 records and if the queries return back more than 50k records than the exception would be thrown. So as to avoid this exception we should write our queries with appropriate filter criteria and also define limit in SOQL. The limit is for all the queries that are executed in one transaction. That means if your transaction has 2 queries one of which returns 40K records and the other returns back 10,001 records then also you would hit the limit.

4. Total number of records processed in a single DML - the limit is 10,000

This means we can do a dml's on a collections having records upto 10,000 and not more in single transaction. Consider this example where in soql query returns back 20,000 records,

for(account acc [select name,id from account]{
     acc.name = 'test1'

}
update acclist


here, if the acclist contains more than 10,000 records then the exception would be thrown. So as to avoid this kind of error make sure you check the size of the list before making the DML.

As an example below, the dml would be fired only if the size of the list is less than 10K and hence would not throw governer limit exception.

for(account acc [select name,id from account]{
     acc.name = 'test1'

}
if(acclist.size()<10000)
update acclist


apart from these 4 mentioned limits there are various other limits that you can refer in salesforce documentation but the above mentioned limits are the ones we come across very often.

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9  ** Article 10
 

No comments:

Post a Comment