
Salesforce Best Practice: No SOQL or DML in Loops
Learn why avoiding SOQL and DML in loops is a critical best practice in Salesforce Apex development. Discover how to optimize your code for efficiency and performance with practical examples and tips.
SALESFORCE BEST PRACTICES
8/9/20242 min read
In Salesforce Apex development, one of the cardinal rules is to avoid performing SOQL (Salesforce Object Query Language) queries and DML (Data Manipulation Language) operations inside loops. This best practice is essential for maintaining efficient, scalable, and governor limit-compliant code. Let’s explore why this is important and how you can adhere to this practice with practical examples.
Why Avoid SOQL and DML in Loops?
1. Governor Limits
Salesforce imposes strict governor limits to ensure efficient processing and fair resource usage. One of these limits is on the number of SOQL queries and DML operations that can be performed in a single transaction. Exceeding these limits can cause your code to fail, resulting in unhandled exceptions and incomplete transactions.
2. Performance
Executing SOQL queries or DML operations within loops can significantly degrade performance. Each query or DML statement inside a loop executes individually, causing excessive server round-trips and processing time.
3. Scalability
Code that performs SOQL or DML operations in loops does not scale well. As data volume increases, the number of queries and operations increases linearly, leading to potential performance bottlenecks and limit violations.
Best Practices to Avoid SOQL and DML in Loops
1. Bulkify Your Code
Bulkification is the process of designing your code to handle multiple records at once. Instead of executing a query or DML operation for each record individually, you perform these operations in bulk.
Example:
Inefficient Code:


Optimized Code:
2. Use Collections
Using collections such as lists, sets, and maps allows you to store multiple records and process them in bulk, minimizing the number of SOQL queries and DML operations.
Example:
3. Optimize SOQL Queries
Combine SOQL queries to retrieve all necessary data at once, reducing the number of queries.
Example:
4. Efficient DML Operations
Group DML operations to process multiple records at once, reducing the number of DML statements.
Example:
Conclusion
Avoiding SOQL and DML in loops is a fundamental best practice in Salesforce Apex development. By bulkifying your code, using collections, optimizing queries, and grouping DML operations, you can ensure your applications are efficient, scalable, and compliant with Salesforce governor limits. Implement these practices to enhance the performance and reliability of your Apex code.
FAQs
1. What are SOQL and DML in Salesforce Apex? SOQL (Salesforce Object Query Language) is used to query data from Salesforce. DML (Data Manipulation Language) operations are used to insert, update, delete, or undelete data.
2. Why is it bad to have SOQL or DML in loops? Performing SOQL or DML in loops can lead to exceeding Salesforce governor limits, degrading performance, and scalability issues.
3. How can I avoid SOQL in loops? You can avoid SOQL in loops by bulkifying your code, using collections to handle multiple records, and optimizing your SOQL queries to retrieve all necessary data at once.
4. What is bulkification in Salesforce? Bulkification is the practice of designing your code to handle multiple records at once, reducing the number of SOQL queries and DML operations.
5. How do I handle large data volumes in Salesforce Apex? Handle large data volumes by using bulkified code, efficient queries, collections, and minimizing the number of SOQL and DML operations within your Apex transactions
