Friday, August 8, 2014

Reasons Salesforce Enforces Governor Limits


Apex runs in a multi-tenant environment. The resources within that multi-tenant environment need to be available to all tenants. To be sure of that, Salesforce’s “run time engine” constantly checks to orgs and app to be sure they are not exceeding Salesforce’s imposed governor limits.


Salesforce imposes these limits to prevent any organization from monopolizing the shared server resources. While writing apex code, there are various possible ways to account for and stay within governor limits. There is also the possibility to raise a case with Salesforce support to increase your governor limits by paying extra money.


In our app, RPaaS, we encountered and had to work with governor limits in several instances.


1– In Apex triggers, when we used DML statements like ‘Insert’, ‘Update’, ‘Upsert’ or ‘Delete’  inside the For Loops; the loop executes  and hits the limit of the SOQL query, since this is executed in single instance.


2– The same limit applies to Send email methods inside of loops and many more....


To avoid problems like examples 1 and 2, we learned to not use statements inside loops.


3– Because of Heap Size Limits; when writing queries, usage of a proper ‘WHERE’ clause will slow down the total number of records returned by each collection type (Lists, Maps and Sets).


To avoid problem #3, make your SOQL Queries selective. See Link #2 for more Info.


Please refer to links below for an extensive list of code enforcements made by Salesforce:


Linke 1: General Definition  


Link 2 : Learn more about the enforcement


Link 3: Best Practice while writing the trigger


Let us build a stronger developer community by sharing our experience! Have you encountered similar issues? How did you handle them?

Nirmal Christopher
SFDC Technical Consultant
Global Tech & Resources| www.GTR.net