Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 2
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    SalesforceCodex
    Home»Salesforce»Apex»How to Handle Bulkification in Apex with Real-World Use Cases

    How to Handle Bulkification in Apex with Real-World Use Cases

    Dhanik Lal SahniBy Dhanik Lal SahniJuly 15, 2025No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Bulkification in Apex | SalesforceCodex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Bulkification in Apex is the discipline of designing code that efficiently processes huge amounts of data in a single execution while adhering to Salesforce governor constraints (e.g., 50,000 records queried, 10,000 DML rows per transaction). Bulkification is crucial in Salesforce since Apex is frequently used in situations such as triggers, batch tasks, and connectors that process several records at once. Poorly bulkified code may exceed governor limitations, cause performance concerns, or fail.

    Key Principles of Bulkification

    1. Process Records in Collections

    Developers should work on collections (List, Set, Map) to handle multiple records at once instead of processing records individually. This approach minimizes governor limit usage and boosts performance.

    2. Minimize SOQL Queries

    All necessary data should be retrieved using a single query. Try to avoid multiple SOQL queries to fetch related data. Instead of separate queries, use relationship queries to fetch related data efficiently.

    3. Minimize DML Operations

    Perform DML operations (insert, update, delete) on collections rather than individual records. Consolidate DML statements outside loops. Use List<sObject> for different types of object collections for DML operations.

    4. Leverage Maps for Efficient Lookups

    Use a Map to store and access related data quickly, and also to avoid repeated queries. Instead of querying the same record multiple times, you can use a Map<Id, sObject> to store query results once and reference them quickly, reducing SOQL calls and improving performance.

    5. Handle Triggers Efficiently

    Triggers can process up to 200 records at a time in bulk operations (e.g., data imports). We should ensure trigger logic scales to handle this. We can use a trigger framework that handles bulk operations. Always create trigger handler classes for logic creation. Don’t write logic in the trigger class code.

    6. Handle Mixed Operations (Insert, Update, Delete) Carefully

    When handling mixed operations (Insert, Update, Delete) in Apex, it’s crucial to separate the logic for each operation type to avoid data conflicts and governor limit issues. Use conditional blocks to check if the trigger is for insert, update, or delete to isolate logic. Additionally, group records into separate lists (e.g., recordsToInsert, recordsToUpdate, recordsToDelete) and perform DML only once per group, after processing is complete. This ensures cleaner logic, avoids duplicate processing, and keeps your code bulk-safe and maintainable.

    7. Avoid DML Statements Inside Loops

    Never place DML statements inside loops to avoid hitting governor limits in Apex. Instead, collect all records that need to be inserted, updated, or deleted into a list during the loop, and perform the DML operation once outside the loop. This approach ensures your code is bulk-safe and avoids the risk of exceeding the limit of 150 DML statements per transaction.

    8. Test with Bulk Data (200+ Records)

    Always write test classes that simulate large data volumes to catch bulkification issues early. Ensure your logic handles Trigger.new, collections, and maps correctly. Always assert that the code processes all records without hitting governor limits and performs bulk-safe DML and SOQL operations.

    Check out Apex Code best practices for other key points.

    Real-World Use Cases with Apex Code Examples

    Use Case 1: Update Related Contacts When Accounts Are Updated

    Scenario: When the Industry field of an Account is updated, all related Contacts’ Description fields should be updated to match the new industry. This logic should be handled by a trigger that supports bulk operations, such as when 200 Accounts are updated through a data import.

    Non-Bulkified Trigger Logic for Contact Field Update

    Issues with the above code:
    • SOQL query inside a loop (hits query limit if many Accounts are processed).
    • DML operation (update) inside a loop (hits DML limit).

    Bulkified Trigger Logic for Contact Field Update

    Why It’s Bulkified:

    • Created separate Trigger and Trigger Handler classes for code best practices.
    • Uses a set to collect AccountId values for querying.
    • Single SOQL query to fetch all related Contacts. Only required fields are fetched.
    • Single DML operation (update) outside the loop. Only the field assignment is done in a loop.
    • Handles bulk data (e.g., 200 Accounts with multiple contacts each).

    Refer to the post Optimizing Loop in Salesforce Apex for loop optimization.

    Use Case 2: Sync Opportunities with External System

    Scenario: When opportunities are updated, sync their Amount and CloseDate to an external system via an API call. The code must handle bulk updates without hitting governor limits.

    Non-Bulkified Code:

    Issues with the above code:

    • SOQL query inside a loop.
    • API call (callout) inside a loop (hits callout limit of 100 per transaction). After 100 callouts, an exception will be thrown.

    Bulkified Code for external callout

    Why It’s Bulkified:

    • Single SOQL query to fetch all related Accounts.
    • Uses a Map for efficient account lookups.
    • Consolidates API payloads into a single call (or batches for large datasets).
    • Handles bulk opportunities efficiently.

    Use Case 3: Batch Apex for Processing Large Datasets

    Scenario: Update the Status__c field of all Cases older than 30 days to ‘Closed’. The dataset is too large for a single transaction, so use Batch Apex.

    Non-Bulkified Apex Code
    Issues in code:
    1. DML Statement in loop

    Bulkified Batch Apex

    Why It’s Bulkified:

    • Uses Batch Apex to process records in chunks (default batch size is 200).
    • Single DML operation per batch.
    • Scales to handle thousands or millions of records without hitting governor limits.

    Summary

    When working with Salesforce Apex, it’s important to follow best coding practices to ensure your code runs reliably at runtime. One key practice is bulkification, which allows your code to handle both single records and large record sets (like data imports) efficiently. This post includes practical examples, but there are many other essential bulkification techniques developers should consider.

    Need an Expert to Code Bulkification?

    Check out our gig – development of Lightning Web Components, Apex, or Integration.

    References

    • SOQL For Loops
    • Batch Apex

    Similar post

    • Optimizing Salesforce Apex Code
    • Limiting data rows for lists
    • Disable Debug Mode in production
    • Caching of the described object in Apex
    • How to Use Filter in SOQL
    • Avoid Heap Size in Salesforce Apex
    • How to Optimize Salesforce Trigger
    • Understanding Foreign Key Relationship in SOQL
    • Defer Sharing Rules
    • Avoid Hardcoding in Apex
    • Use Platform Caching to enhance performance

    apex apex code best practice apex code bulkification apex code optimization code bulkification code optimization hardcoded ids in code optimize apex optimize apex code optimize code salesforce salesforce apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Confidently Manage Transactions in Salesforce Apex
    Next Article The Ultimate Guide to Apex Order of Execution for Developers
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 Salesforce Developer Blog by SalesforceBen.com
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.