Sub topic 5
Why Triggers Always Receive a List
This chapter assumes you know basic Apex syntax, sObjects, SOQL, and DML, and that triggers fire on insert, update, delete, and undelete. It explains why a trigger gets a collection of records and what that means for how you write it.
One trigger call, many records
A trigger does not run once per record. Salesforce runs it once per batch of records. Trigger.new is always a List, even when the user saves a single record in the UI. In that case the list has one item.
Many operations send more than one record at a time:
- Data Loader, which sends up to 200 records per batch.
- The Bulk API, which splits large jobs into chunks of 200.
- Apex DML such as
insert accounts;, whereaccountsholds many records. - Imports, mass updates, and integrations.
When a single operation contains more than 200 records, Salesforce splits it into chunks of up to 200. It then runs the trigger once for each chunk. All chunks in the same transaction share one set of governor limits.
What this means for your code
A trigger written for one record works in a simple test and fails in production. The code below runs one SOQL query for each record.
trigger ContactTrigger on Contact (before insert) {
for (Contact c : Trigger.new) {
// Anti-pattern: one SOQL query per record
Account a = [SELECT Id, Industry FROM Account WHERE Id = :c.AccountId];
if (a.Industry == 'Banking') {
c.Description = 'Banking contact';
}
}
}With one contact, it uses one query. With 101 contacts in a synchronous transaction, it exceeds the limit of 100 SOQL queries and throws a System.LimitException. This code also throws an error when AccountId is null, because the query returns no row.
Common errors
- Reading only the first record. Code such as
Trigger.new[0]ignores the other records in the batch. Loop over the whole list. - Testing with one record. A test that inserts a single record passes even when the trigger fails at 101 records. Chapter content on bulk testing covers how to test at 200.
- Assuming a trigger call equals a user action. One user action can produce several trigger calls. One trigger call can also hold records from several user actions, for example when an import runs.
- Putting SOQL or DML in a loop. Each pass through the loop spends from a limit that the whole transaction shares.
The habit to build
Treat Trigger.new as a collection to process as a whole. Read through the list first to gather what you need, run queries and DML outside the loops, and then apply the results. The next chapters show how to do this with Sets, Maps, and single DML statements.
Exercise
A user imports 450 Contact records with the Bulk API. How many times does the Contact trigger run, and how many records does each run receive at most?
Show answer
The trigger runs three times, once for each chunk of up to 200 records (200, 200, and 50 records). All three runs share one set of governor limits if they belong to the same transaction.
Exercise
A trigger with a SOQL query inside a for loop over Trigger.new passes a test that inserts one record. Why can it still fail in production?
Show answer
The query runs once per record. With 101 or more records in a synchronous transaction, the trigger exceeds the limit of 100 SOQL queries and throws a LimitException. A one-record test never reaches that limit.