Salesforce Decode
Salesforcedecode
Back to blogapex

Apex Bulkification: Processing Millions of Records Safely

By Avijit Patra10 min read

Master bulkification patterns for triggers, batch jobs, and queueable chains when dealing with large data volumes.

Why Bulkification Matters


Salesforce executes triggers and classes in batches of up to 200 records. Code that works for one record often fails at scale.


Trigger Best Practices


  • Never query or DML inside loops
  • Use Maps and Sets for O(1) lookups
  • Collect records for single bulk DML at the end
  • Implement recursion guards with static flags

  • Large Data Volume Strategy


    For 5M+ records daily:

    1. **Batch Apex** — Process in chunks of 200 with stateful tracking

    2. **Queueable Chain** — Chain jobs for dependent processing

    3. **Platform Events** — Decouple producers from consumers

    4. **Custom Indexing** — External ID fields for efficient queries


    Performance Tips


  • Select only needed fields in SOQL
  • Use FOR UPDATE only when necessary
  • Consider async for non-critical path operations
  • Monitor Apex CPU time and heap size in debug logs
  • apexbulkificationgovernor-limits