Governor Limits in Salesforce

Governor Limits in Salesforce

Salesforce stands as a titan, serving over 150,000 companies worldwide as of 2024. With such a vast user base, ensuring optimal performance and fair resource allocation is crucial. This is where Governor Limits come into play. 

What are Governor Limits in Salesforce?

Governor Limits are restrictions imposed by Salesforce to ensure that no single organization monopolizes shared resources in the multi-tenant environment. These limits help maintain system stability, prevent resource hogging, and ensure fair usage across all Salesforce customers.

Types of Governor Limits

Salesforce implements various types of governor limits to control different aspects of resource usage. Let’s explore the main categories:

Per-Transaction Apex Limits:

These limits apply to individual Apex transactions and include:

  • SOQL queries: 100 per transaction
  • DML statements: 150 per transaction
  • CPU time: 10,000 milliseconds in synchronous Apex

Force.com Platform Limits:

These limits apply to the entire org and include:

  • API requests: Varies based on edition, typically 15,000 – 5,000,000 per day
  • Custom apps: 10 – unlimited, depending on the edition
  • Custom objects: 50 – unlimited, depending on the edition

Static Apex Limits:

These limits are constant across all transactions and include:

  • Maximum execution time for synchronous Apex: 10 minutes
  • Maximum number of characters for a Visualforce page: 1,000,000

Size-Specific Apex Limits:

These limits are based on the size of the data being processed:

  • Maximum size of serialized data for calls to API methods: 6 MB
  • Maximum size of a SOQL query result: 50,000 records

Email Limits

  • Single email: 5,000 external recipients per day
  • Mass email: 5,000 recipients per day, with a maximum of 1,000 per email

Push Notification Limits

  • 50,000 push notifications per app per day

Best Practices to Handle Governor Limits

To effectively manage Salesforce Governor Limits and optimize your organization’s performance, consider implementing the following best practices:

Use Bulk API for large data operations

  • Leverage the Bulk API when inserting, updating, or deleting over 2000 records.
  • Use CSV files for data loading to reduce API calls and improve efficiency.
  • Implement parallel processing with the Bulk API to handle millions of records.

getgenerativeai

Implement SOQL for loops to process large data sets

  • Use SOQL for loops (for (Account acc : [SELECT Id, Name FROM Account])) instead of querying all records at once.
  • This approach helps avoid hitting heap size limits and allows processing of up to 50 million records.

Utilize batch Apex for processing large volumes of records

  • Implement the Database.Batchable interface for jobs that process more than 50,000 records.
  • Break down large jobs into smaller chunks using the scope parameter in the start method.
  • Use the Database.Stateful interface to maintain state across batch executions.

Cache frequently used data to reduce SOQL queries

  • Implement custom caching mechanisms using static variables or custom settings.
  • Use Platform Cache for frequently accessed, rarely changing data to reduce database calls.
  • Leverage the @AuraEnabled(cacheable=true) annotation for Lightning component methods to cache server-side data.

Use platform events for asynchronous processing

  • Decouple time-intensive operations using platform events to avoid synchronous processing limits.
  • Implement a publish-subscribe model for real-time data updates across the org.
  • Use platform events to integrate with external systems without hitting API call limits.

Optimize SOQL queries

  • Use selective queries with proper indexing to improve query performance.
  • Avoid using SOQL queries inside loops; instead, gather all necessary data in a single query.
  • Utilize relationship queries to reduce the number of separate SOQL calls.

Implement efficient DML operations

  • Use bulk DML operations instead of iterative DML statements.
  • Leverage the Database.SaveResult class to handle partial successes in DML operations.
  • Use Database.upsert() instead of separate insert and update operations when applicable.

Leverage @future methods for asynchronous processing

  • Use @future methods for operations that can be processed asynchronously, such as callouts to external web services.
  • Implement @future(callout=true) for methods that make HTTP callouts to avoid mixed DML operation errors.

Optimize Visualforce pages and controllers.

  • Use lazy loading techniques in Visualforce controllers to defer data retrieval until necessary.
  • Implement pagination for large data sets to reduce view state size and improve page load times.
  • Utilize the transient keyword for variables that are not part of the view state.

Implement efficient trigger design

  • Use a single trigger per object to consolidate logic and reduce the risk of hitting governor limits.
  • Implement bulkification in triggers to handle multiple records efficiently.
  • Use helper classes to modularize trigger logic and improve maintainability.

Utilize the Queueable interface for complex asynchronous jobs

  • Use the Queueable interface for jobs that require more complex logic than @future methods allow.
  • Chain Queueable jobs for multi-step asynchronous processes while respecting governor limits.

Implement error handling and retry mechanisms.

  • Use try-catch blocks to handle exceptions gracefully and prevent unnecessary consumption of governor limits.
  • Implement retry mechanisms with exponential backoff for operations that may fail due to temporary issues.

By implementing these best practices, you can significantly improve your Salesforce org’s performance, reduce the risk of hitting governor limits, and ensure a smooth user experience even as your data and processes grow in complexity.

Also Read – Relationships in Salesforce: A Complete Guide

How can I optimize my Apex code to avoid hitting governor limits?

  1. Avoid SOQL queries inside loops
  2. Use selective queries to retrieve only necessary data
  3. Implement the Aggregate-Query-Update pattern
  4. Leverage @future methods for asynchronous processing
  5. Use collections and bulk DML operations instead of iterative processing

Are there any tools to monitor governor limits in real time?

Yes, Salesforce provides several tools to monitor governor limits:

  1. Debug Logs: View detailed execution logs, including limit usage
  2. Salesforce CLI: Use the force:apex:log:get command to retrieve logs
  3. Developer Console: Monitor limit usage in real-time during code execution
  4. Salesforce Einstein Analytics: Create dashboards to track API usage and other limits

How do certified managed packages affect governor limits?

Certified managed packages have their governor limits, separate from your organization’s limits. This means:

  1. They don’t consume your org’s limits for most operations
  2. They have their own SOQL query and DML statement limits
  3. CPU time is shared between the package and your org

However, some limits, like the total number of SOQL queries per transaction, still apply globally.

Related Read – Salesforce Implementation With AI – The Ultimate Guide

Conclusion

Understanding and effectively managing Salesforce Governor Limits is crucial for maintaining optimal performance in your Salesforce org. By following best practices, optimizing your code, and leveraging the right tools, you can ensure that your Salesforce implementation runs smoothly and efficiently, even as your organization grows and your processes become more complex.

Enhance your Salesforce consulting with GetGenerative.ai. Effortlessly craft outstanding proposals, enabling you to dedicate more time to providing exceptional client service. 

Start today!

Frequently Asked Questions (FAQs)

1. Can governor limits be increased? 

Some limits can be increased by purchasing additional licenses or contacting Salesforce support, but many core limits are fixed for system stability.

2. Do governor limits apply to all Salesforce editions? 

Yes, but the specific limits may vary depending on the edition.

3. What happens if I hit a governor limit? 

The transaction will fail, and you’ll receive a system exception.

4. Do governor limits apply to Lightning components? 

Lightning components are subject to governor limits, especially when making server-side calls.

5. How often are governor limits updated? 

Salesforce reviews and updates governor limits with major releases, typically three times a year.