In this session, we’ll delve into the realm of Salesforce Governor Limits and explore strategies for overcoming them. These limits pose a significant hurdle for Salesforce developers, as surpassing the defined thresholds in Apex code triggers Salesforce to enforce these governor limits, resulting in an unhandled runtime exception. Consequently, meticulous attention is required during application development to ensure adherence to these limits and prevent potential issues.

What is Governor Limit in Salesforce?

Think of Governor Limits in Salesforce like rules that keep things fair in a shared playground. In Salesforce, everyone’s data is stored in one big database, but we don’t want one person taking up all the swings. So, to make sure everyone gets a fair turn, Salesforce sets limits on how much data each person can use. These rules are called Governor Limits, and they’re enforced by the Apex run-time engine to keep everything running smoothly in the Salesforce neighborhood.

Types of Governor Limits in Salesforce

CategoryDescription
Per-Transaction Apex LimitsCounted for each Apex transaction; reset for each execution in Batch Apex’s execute method.
Force.com Platform Apex LimitsEnforced by the Lightning platform; not specific to an Apex transaction.
Static Apex LimitsApplied across all transactions.
Size-Specific Apex LimitsLimits related to the size of code.
Miscellaneous Apex LimitsVarious limits within the Apex environment not covered by other categories.
Email LimitsLimits related to sending emails through Apex.
Push Notification LimitsLimits associated with the use of push notifications in Apex.

Salesforce Governor Limits Cheat Sheet

Here are some critical Salesforce governor limits that you should know when planning to enter the Salesforce field or working on some Salesforce project. Here are different types of governor limits.

Per-Transaction Apex Limits

DescriptionSynchronous
Governor Limit
Asynchronous Limit
Total number of SOQL queries issued1100200
Total number of SOSL queries issued in Salesforce20
Total number of DML statements issued150
Total number of records retrieved by a single SOSL query2000
Total number of records retrieved by SOQL queries50000
Total number of records retrieved by Database.getQueryLocator10000
Total heap size6 MB/12 MB
Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction120 seconds
Maximum number of Apex jobs added to the queue with System.enqueueJob501
Maximum CPU time on the Salesforce servers510,000 milliseconds60,000 milliseconds

Salesforce Developer Edition:

DescriptionMaximum Usage Limit
Schema
CUSTOM OBJECTS400
DATA STORAGE5.0 MB
Business Logic
RULES2,000
CODE USED3,000,000 characters
Most Used Licenses
SALESFORCE PLATFORM3
SALESFORCE2
CHATTER FREE5,000
API Usage
API REQUESTS, LAST 24 HOURS15,000
User Interface
CUSTOM APPS10
ACTIVE SALESFORCE SITES1
ACTIVE FLOW500
CUSTOM TABS100

Salesforce Enterprise Edition:

DescriptionMaximum Usage Limit
Schema
CUSTOM OBJECTS200
DATA STORAGE200MB
Business Logic
RULES2000
CODE USED3,000,000 characters
Most Used Licenses
SALESFORCE PLATFORM9
SALESFORCE181
CUSTOMER COMMUNITY PLUS1
API Usage
API REQUESTS, LAST 24 HOURS5,000,000
User Interface
CUSTOM APPS260
ACTIVE SALESFORCE SITES25
ACTIVE FLOW500
CUSTOM TABS1225

Salesforce Professional Edition:

DescriptionMaximum Usage Limit
Schema
CUSTOM OBJECTS50
DATA STORAGE1GB
Business Logic
RULES2000
CODE USED3,000,000 characters
Most Used Licenses
CHATTER EXTERNAL500
SALESFORCE5
CHATTER FREE5000
API Usage
API REQUESTS, LAST 24 HOURS16000
User Interface
CUSTOM APPS255
ACTIVE SALESFORCE SITES
ACTIVE FLOW500
CUSTOM TABS1210

Salesforce Unlimited Edition:

DescriptionMaximum Usage Limit
Schema
CUSTOM OBJECTS2000
DATA STORAGE1GB
Business Logic
RULES2000
API Usage
API REQUESTS, LAST 24 HOURSUnlimited
User Interface
CUSTOM APPSUnlimited
ACTIVE FLOW500
CUSTOM TABSUnlimited

Static Apex Limits

DescriptionLimits
The default timeout of callouts (either HTTP requests or Web services calls) in a transaction10 seconds
The maximum size of the callout request or response (either HTTP request or Web services call)6 MB for synchronous Apex or 12 MB for asynchronous Apex
The maximum SOQL query runtime before Salesforce cancels the transaction120 seconds
The maximum number of class and trigger code units in the deployment of Apex7500
Apex trigger batch size200
For loop list batch size200
The maximum number of records that are returned for a Batch Apex query in Database.QueryLocator50 million

Advantages of Governor Limits in Salesforce

Why did Salesforce bring in the Salesforce governor limit? Well, it’s got some perks. Let’s break them down.

  1. Sharing the Playground: Imagine if one person could hog all the swings in the Salesforce playground. Governor limits prevent that by stopping lengthy code from taking up too much space in the memory or hogging the entire cloud CPU.
  2. Apex’s Special Rules: Apex, Salesforce’s coding language, has its own set of rules. Governor limits make sure we play by those rules to keep everything running smoothly.
  3. Staying in the Coding Lane: These limits act like traffic rules for coding in Apex. They help us stay in the right lane, avoiding chaos and ensuring a smooth coding journey.

How to overcome governor limits in Salesforce

Apex developers can use certain workarounds and platform-specific best practices to avoid hitting hard governor limits in their code. Here’s a list of five tips to try.

#Best PracticeExplanation
1Single Trigger ObjectsHave only one Trigger for each Apex object. Writing multiple Triggers may lead to unpredictable execution order and could hit transaction limits.
2Write Trigger Logic in Your Apex ClassesTo ensure testability and avoid governor limits, place Trigger Logic in an Apex Class known as a Trigger Handler. This allows test runs to access methods written inside Triggers.
3Context-Specific Handler MethodsIf your Trigger has multiple context conditions (e.g., IF before and after), create separate handler methods for each context. This enhances code readability and aids in troubleshooting limits.
4Use Collections and Limit FOR Loops in Your CodeAvoid bloating your code size and exceeding synchronous transaction limits by excluding SOQL and DML from FOR loops. Utilize Collections for multiple records to limit transactions.
5SOQL Query FOR Loops for Large Data SetsWhen dealing with large data sets, use SOQL Query FOR Loops to prevent exceeding the 50,000 total records limit. This allows querying data in batches and avoids hitting heap limits.