Sub topic 2
Loading the record and applying new values
This chapter assumes you can write a basic Apex trigger, know how objects, fields, and records relate, and understand insert, update, and upsert DML. It covers the first two steps of the order of execution. Salesforce first gets the starting version of the record, then overlays the values from the request. Every later step works on the result.
What happens first
A save request can come from the standard UI, an API call, a Flow, or Apex DML. Whatever the source, Salesforce does two things before any automation runs:
- It loads the original record from the database. For an insert there is no original, so Salesforce initializes a new empty record. For an upsert, Salesforce first decides whether the record exists, then follows the update or insert path.
- It loads the new field values from the request and overwrites the old values on that in-memory record.
The record that results from step 2 is what before-save flows and before triggers see. It is also what ends up in Trigger.new.
Only the fields in the request are overwritten
On an update, the request is a set of changes, not a full replacement of the record. Fields the request does not mention keep the values loaded from the database. This is why Trigger.new in an update trigger contains complete records, not just the changed fields.
The standard UI sends the fields on the edit page layout. A field that is not on the layout is never overwritten by a UI save. In Apex, a field counts as part of the request only if your code assigned it, even if the value is null.
The following code shows this. Deploy the trigger, then run the anonymous Apex below it.
trigger AccountChangeInspector on Account (before update) {
for (Account newAcc : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(newAcc.Id);
System.debug('Name: old=' + oldAcc.Name + ' new=' + newAcc.Name);
System.debug('Phone: old=' + oldAcc.Phone + ' new=' + newAcc.Phone);
System.debug('Website: old=' + oldAcc.Website + ' new=' + newAcc.Website);
}
}Account a = new Account(Name = 'Acme', Phone = '555-0100', Website = 'https://acme.example');
insert a;
// Only Phone is part of this request.
Account change = new Account(Id = a.Id, Phone = '555-0199');
update change;The debug output shows Name and Website identical in old and new, because Salesforce took them from the loaded record. Only Phone differs.
To clear a field, assign null explicitly:
Account clearSite = new Account(Id = [SELECT Id FROM Account WHERE Name = 'Acme' LIMIT 1].Id);
clearSite.Website = null;
update clearSite;Because Website was assigned, it is part of the request and is overwritten with null. Leaving the line out would leave the website unchanged.
What each operation gives you
The starting point differs by operation, and that determines what the trigger context variables hold.
| Operation | Original record loaded? | Trigger.old / Trigger.oldMap | Trigger.new Id |
|---|---|---|---|
| Insert | No, a blank record is initialized | `null` | `null` in before triggers |
| Update | Yes, by Id | Original values | Existing Id |
| Upsert, match found | Yes, treated as an update | Original values | Existing Id |
| Upsert, no match | No, treated as an insert | `null` | `null` in before triggers |
Upsert matches on the record Id, or on an external ID field if you specify one. The match decides which path the request takes, so the same upsert statement can fire insert triggers for some records and update triggers for others.
An insert has no stored record yet, so the Id does not exist at this point. This is the root of the rule that Id is unavailable in a before insert trigger. Salesforce assigns it later, when the record is saved to the database.
Batches and the loaded copy
Salesforce processes DML in chunks of up to 200 records. Each chunk loads originals, applies values, and runs the full sequence before the next chunk starts. A single update of 450 records therefore calls your trigger three times, with 200, 200, and 50 records. Triggers must be written for a list of records, not for one.
Comparing old and new reliably
Because Trigger.new holds the original values for untouched fields, comparing Trigger.new with Trigger.oldMap finds real changes. Writing the same value to a field in the request is not a change, so a comparison shows no difference.
trigger AccountPhoneChange on Account (before update) {
for (Account a : Trigger.new) {
if (a.Phone != Trigger.oldMap.get(a.Id).Phone) {
a.Description = 'Phone changed on ' + System.now();
}
}
}Two caveats apply:
- Values overwritten at this step are not final. Before-save flows and before triggers can still change
Trigger.new, soTrigger.oldis the only stable record of the original state. - Formula fields are calculated from other fields, not stored with the request. You cannot overwrite them, and their values in
Trigger.newduring a before trigger may not reflect the incoming changes.
Common errors
- Assuming a field is missing or
nullinTrigger.newwhen it was left out of the request. It holds its stored value. - Reading
Trigger.oldin an insert trigger. It isnull, so this causes a null pointer exception. - Expecting a UI save to clear a field that is not on the page layout. It is never part of that request.
- Using
Trigger.new[0].Idin a before insert trigger. It isnull.
Exercise
A user edits an Account in the standard UI and changes only Phone. The Website field is not on the page layout and currently holds a value. In a before update trigger, what is Trigger.new[0].Website, and how does it compare to Trigger.oldMap.get(Trigger.new[0].Id).Website?
Show answer
Both values are the same stored website. Salesforce loaded the original record from the database, and the UI request did not include Website, so that field was never overwritten. Only Phone differs between Trigger.new and Trigger.old.
Exercise
A trigger on Account (before insert) reads Trigger.old.size(). What happens, and why?
Show answer
It throws a null pointer exception. For an insert there is no original record to load, so Salesforce initializes a blank record and Trigger.old is null. Only update and delete triggers have old values.