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
Category
Description
Per-Transaction Apex Limits
Counted for each Apex transaction; reset for each execution in Batch Apex’s execute method.
Force.com Platform Apex Limits
Enforced by the Lightning platform; not specific to an Apex transaction.
Static Apex Limits
Applied across all transactions.
Size-Specific Apex Limits
Limits related to the size of code.
Miscellaneous Apex Limits
Various limits within the Apex environment not covered by other categories.
Email Limits
Limits related to sending emails through Apex.
Push Notification Limits
Limits 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
Description
Synchronous Governor Limit
Asynchronous Limit
Total number of SOQL queries issued1
100
200
Total number of SOSL queries issued in Salesforce
20
Total number of DML statements issued
150
Total number of records retrieved by a single SOSL query
2000
Total number of records retrieved by SOQL queries
50000
Total number of records retrieved by Database.getQueryLocator
10000
Total heap size
6 MB/12 MB
Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction
120 seconds
Maximum number of Apex jobs added to the queue with System.enqueueJob
50
1
Maximum CPU time on the Salesforce servers5
10,000 milliseconds
60,000 milliseconds
Salesforce Developer Edition:
Description
Maximum Usage Limit
Schema
CUSTOM OBJECTS
400
DATA STORAGE
5.0 MB
Business Logic
RULES
2,000
CODE USED
3,000,000 characters
Most Used Licenses
SALESFORCE PLATFORM
3
SALESFORCE
2
CHATTER FREE
5,000
API Usage
API REQUESTS, LAST 24 HOURS
15,000
User Interface
CUSTOM APPS
10
ACTIVE SALESFORCE SITES
1
ACTIVE FLOW
500
CUSTOM TABS
100
Salesforce Enterprise Edition:
Description
Maximum Usage Limit
Schema
CUSTOM OBJECTS
200
DATA STORAGE
200MB
Business Logic
RULES
2000
CODE USED
3,000,000 characters
Most Used Licenses
SALESFORCE PLATFORM
9
SALESFORCE
181
CUSTOMER COMMUNITY PLUS
1
API Usage
API REQUESTS, LAST 24 HOURS
5,000,000
User Interface
CUSTOM APPS
260
ACTIVE SALESFORCE SITES
25
ACTIVE FLOW
500
CUSTOM TABS
1225
Salesforce Professional Edition:
Description
Maximum Usage Limit
Schema
CUSTOM OBJECTS
50
DATA STORAGE
1GB
Business Logic
RULES
2000
CODE USED
3,000,000 characters
Most Used Licenses
CHATTER EXTERNAL
500
SALESFORCE
5
CHATTER FREE
5000
API Usage
API REQUESTS, LAST 24 HOURS
16000
User Interface
CUSTOM APPS
255
ACTIVE SALESFORCE SITES
—
ACTIVE FLOW
500
CUSTOM TABS
1210
Salesforce Unlimited Edition:
Description
Maximum Usage Limit
Schema
CUSTOM OBJECTS
2000
DATA STORAGE
1GB
Business Logic
RULES
2000
API Usage
API REQUESTS, LAST 24 HOURS
Unlimited
User Interface
CUSTOM APPS
Unlimited
ACTIVE FLOW
500
CUSTOM TABS
Unlimited
Static Apex Limits
Description
Limits
The default timeout of callouts (either HTTP requests or Web services calls) in a transaction
10 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 transaction
120 seconds
The maximum number of class and trigger code units in the deployment of Apex
7500
Apex trigger batch size
200
For loop list batch size
200
The maximum number of records that are returned for a Batch Apex query in Database.QueryLocator
50 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.
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.
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.
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 Practice
Explanation
1
Single Trigger Objects
Have only one Trigger for each Apex object. Writing multiple Triggers may lead to unpredictable execution order and could hit transaction limits.
2
Write Trigger Logic in Your Apex Classes
To 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.
3
Context-Specific Handler Methods
If 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.
4
Use Collections and Limit FOR Loops in Your Code
Avoid 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.
5
SOQL Query FOR Loops for Large Data Sets
When 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.