Intermediate 15 min read

Testing Bulk Operations

By the end of this lesson, you'll be able to:

  • Write a test that exercises trigger logic against 200 records at once
  • Explain why testing with a single record can hide bulk-related bugs

Prerequisites: Code Coverage Requirements

Why one record isn't enough

SOQL/DML-inside-a-loop code often works fine with a single record, since the loop only runs once — it passes review and single-record tests, then hits governor limits the moment someone does a real bulk data load or Data Loader import in production. A bulk-safe test is the only way to catch this before it ships.

Writing a bulk test

Build a List of 200 records — the real maximum trigger batch size — in a loop, insert them all in a single DML statement, then assert the expected outcome across the whole batch, not just one spot-checked record.

A realistic 200-record bulk test

@isTest
static void triggerHandlesA200RecordBulkInsert() {
    List<Account> accounts = new List<Account>();
    for (Integer i = 0; i < 200; i++) {
        accounts.add(new Account(Name = 'Bulk Account ' + i));
    }

    Test.startTest();
    insert accounts;
    Test.stopTest();

    List<Account> inserted = [SELECT Id FROM Account];
    System.assertEquals(200, inserted.size());
}

200 is the real batch size Salesforce uses for trigger invocations — a test at this scale would immediately fail if the trigger handler had SOQL or DML inside a per-record loop.

Exercise

Write a test that inserts 200 Contacts in one bulk DML statement and asserts all 200 were created.

Show hint

Build the List<Contact> in a for loop, then use a single insert statement outside the loop.

APEX

Testing Bulk Operations — Quick Check

1. Why does a single-record test sometimes miss governor limit bugs?

2. 200 records is the real batch size Salesforce uses when invoking a trigger, making it a realistic bulk test size.

3. What's the correct pattern for a bulk test's DML?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A test that only inserts one record can pass even when the underlying code has SOQL or DML inside a loop — testing with a full bulk batch (200 records, matching Salesforce's actual trigger batch size) is the only reliable way to catch that class of bug before production.