Sub topic 9
Testing with 200 Records to Prove Bulk Safety
This chapter assumes you can write basic Apex, know what a trigger is, and understand governor limits at a high level. You should also know how to write a test method with @isTest and Assert.
Why bulk testing matters
Apex code rarely runs on one record at a time. A data load, an API call or a flow can send many records to your code in one transaction. Triggers receive records in batches of up to 200. If your code runs a query or DML statement inside a loop, one record works fine and 200 records can break the transaction.
Governor limits are the platform's per-transaction caps. Two matter most here: 100 SOQL queries and 150 DML statements per synchronous transaction. A test with one record cannot show that your code respects them.
A bulk test for a trigger
The trigger below rates an account as Hot when its annual revenue is above 1,000,000. It loops over Trigger.new and does no queries, so it is bulk safe. The test proves that behavior.
trigger AccountTrigger on Account (before insert) {
for (Account a : Trigger.new) {
if (a.AnnualRevenue != null && a.AnnualRevenue > 1000000) {
a.Rating = 'Hot';
}
}
}
@isTest
private class AccountTriggerTest {
@isTest
static void marksHighRevenueAccountsHotInBulk() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
Decimal revenue = Math.mod(i, 2) == 0 ? 2000000 : 500;
accounts.add(new Account(Name = 'Acme ' + i, AnnualRevenue = revenue));
}
Test.startTest();
insert accounts;
Test.stopTest();
Integer hotCount = [SELECT COUNT() FROM Account WHERE Rating = 'Hot' AND Id IN :accounts];
Assert.areEqual(100, hotCount, 'Only the high-revenue half should be rated Hot');
}
}What the test does
- It builds 200 accounts in a list. Even-numbered records get high revenue and odd-numbered records get low revenue.
- It inserts the whole list with one DML statement. The trigger sees all 200 records in a single
Trigger.new. Test.startTest()andTest.stopTest()surround the insert. The code between them gets a fresh set of governor limits, so setup work does not count against the code under test.- It queries for Hot accounts and asserts exactly 100.
The mixed data matters. If every record qualified, a bug that updated all records, or none, could slip through. Splitting the data lets the assertion show that the code treated each record on its own.
What a bulk test catches
Consider a version of the trigger that queries inside the loop, for example to look up a related record for each account. With one record it uses one query and passes. With 200 records it reaches the 101st query and throws a System.LimitException. The test fails, and you find the problem before production does.
A bulk test can fail in two ways:
- A limit exception, because the code does a query or DML statement per record.
- A wrong assertion count, because the code handles some records incorrectly when they arrive together.
Practical guidelines
- Use 200 records, because that is the batch size a trigger receives.
- Mix records that should and should not match the logic, and assert on both groups where it helps.
- Make one DML call with the full list.
- Put the DML and the code under test between
Test.startTest()andTest.stopTest(). - Assert on outcomes, not only that no exception occurred.
Exercise
A developer writes a test that loops 200 times and calls insert new Account(Name = 'Test ' + i); inside the loop. The trigger under test runs a SOQL query for each record in Trigger.new. Will the test show the bulk problem? Why or why not?
Show answer
No. Each insert fires the trigger with a single record, so the trigger runs one query per transaction call and never approaches the 100-query limit within one trigger execution. The test should add all 200 accounts to a list and insert the list in one DML statement. Then the trigger receives 200 records and the per-record query hits the limit.
Exercise
Why does the example test give half of the accounts high revenue and half low revenue instead of giving all 200 high revenue?
Show answer
Mixed data lets the assertion confirm that the code changed only the matching records. If all records matched, a bug that rated every account Hot would still pass.