
Drawbacks in governor limits of Salesforce?

Table of Contents:
- Governor Limits
- Overcoming Governor Limits
- Best Solution
- Code Example
- Improved Code Example
- Explanation of the Improvement
Salesforce imposes Governor Limits to ensure that resources are shared fairly among all applications running on the multi-tenant platform. However, these limits can sometimes be challenging for developers. Here’s a breakdown of the drawbacks and how to overcome them:
Drawbacks of Governor Limits
- Performance Constraints: Limits on CPU time, heap size, and SOQL queries can restrict the performance and scalability of applications.
- Complex Code: Developers often need to write more complex and less intuitive code to work within the limitations.
- Reduced Flexibility: Limits on DML statements and future method calls can make it challenging to implement complex business processes.
- Testing and Debugging Difficulty: The need to bulkify code and handle limits makes testing and debugging more complex.
- Integration Challenges: When integrating with other systems, governor limits can restrict the volume of data being processed or the frequency of transactions.
Watch our FREE Salesforce online course video, it’s a full length free tutorial for beginners.
Overcoming Governor Limits
- Bulkify Your Code: Ensure that your code properly handles bulk operations to avoid hitting SOQL and DML limits.
- Use Efficient SOQL Queries: Optimize your queries and avoid unnecessary queries inside loops.
- Leverage Asynchronous Operations: Use @future methods, Queueable, Batch, or Scheduled Apex to handle longer-running processes.
- Optimize Resource Usage: Monitor and optimize the use of CPU time and memory (heap size).
- Use Collection Variables Wisely: Utilize Maps, Lists, and Sets to efficiently handle data and reduce the need for SOQL queries.
- Implement Effective Error Handling: Design your application to gracefully handle situations where limits are approached or exceeded.
Best Solution
The best solution often depends on the specific needs and context of your Salesforce application. Generally, adhering to best practices in Apex development and architecture is crucial. This includes understanding and efficiently working within the governor limits, optimizing code, and using Salesforce features like batch classes, future methods, and the Bulk API for larger data operations. Continuous monitoring and refactoring of the codebase as the application evolves and as Salesforce updates its platform are also key to effectively managing and overcoming governor limits.
Code Example
Let’s consider a common scenario where Governor Limits can cause issues in Salesforce Apex code: querying records inside a loop. This can quickly hit the SOQL query limit if not handled properly.
Problematic Code Example:
for (Account acc : [SELECT Id, Name FROM Account]) {
List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE AccountId = :acc.Id];
// Operations on contacts
}
Problems with This Code:
SOQL Query Inside Loop: The SOQL query is inside a for loop. If there are many accounts, this will execute a SOQL query for each iteration and quickly hit the Governor Limit for the maximum number of SOQL queries (100 in a synchronous transaction).
How to Overcome the Problem:
We can refactor the code to query all the necessary data at once and then process it, a technique often referred to as “bulkifying” the code.
Improved Code Example:
// Step 1: Query all accounts
List<Account> accounts = [SELECT Id, Name FROM Account];
// Step 2: Collect all Account Ids
Set<Id> accountIds = new Set<Id>();
for (Account acc : accounts) {
accountIds.add(acc.Id);
}
// Step 3: Query all contacts related to those accounts at once
Map<Id, List<Contact>> accountContactsMap = new Map<Id, List<Contact>>();
for (Contact con : [SELECT Id, Name, AccountId FROM Contact WHERE AccountId IN :accountIds]) {
if (!accountContactsMap.containsKey(con.AccountId)) {
accountContactsMap.put(con.AccountId, new List<Contact>());
}
accountContactsMap.get(con.AccountId).add(con);
}
// Step 4: Process the contacts for each account
for (Account acc : accounts) {
List<Contact> contacts = accountContactsMap.get(acc.Id);
// Operations on contacts
}
Explanation of the Improvement:
- Bulk Data Retrieval: All the relevant records are retrieved in bulk before the loop. This approach ensures that only two SOQL queries are used regardless of the number of accounts, significantly reducing the risk of hitting the SOQL query limit.
- Data Structuring: A
Map
is used to relate accounts to their respective contacts, which is more efficient than querying inside the loop. - Processing: Operations on contacts are done after data retrieval, ensuring that SOQL queries are not executed within a loop.
By restructuring the code in this way, we adhere to Salesforce best practices, avoid hitting Governor Limits, and ensure that the code is scalable and efficient.
For those looking for Salesforce learning, CRS Info Solutions provides an extensive Salesforce training program designed to enhance your skills and career opportunities. Explore our Salesforce training in Bangalore to gain practical, hands-on experience. Our training covers all essential aspects of Salesforce, ensuring comprehensive learning. With expert instructors and a detailed curriculum, CRS Info Solutions is committed to your success in the Salesforce ecosystem with our Career Building program. Whether you are a beginner or looking to advance your skills, they offer the guidance and resources you need. Enroll for a free demo today!