Sub topic 3
System validation before triggers run
This chapter assumes you know what a before trigger is, how validation rules behave, and how DML statements return errors. It covers the check that runs right after Salesforce applies the new values and before any before-save automation runs.
Where this step sits
Salesforce loads the original record, or creates an empty one for an insert. It then overwrites the record's fields with the incoming values. Next comes system validation, the first gate in the save.
What system validation checks
System validation enforces the rules built into the platform, not rules you wrote:
- Required fields have a value.
- Field values match their data type and format, such as a valid date or email address.
- Text does not exceed the field's maximum length.
- For saves from the Salesforce user interface, fields marked required on the page layout also have a value.
The layout check is the one that depends on how the save started. A record saved through Apex or the API skips layout-specific rules. A user saving the same record in the UI does not.
Seeing a system validation error
An insert with a missing required field fails with a system error. Run this as anonymous Apex:
Account a = new Account(); // Name is required and is missing
Database.SaveResult sr = Database.insert(a, false);
System.debug('Success: ' + sr.isSuccess());
for (Database.Error e : sr.getErrors()) {
System.debug(e.getStatusCode() + ': ' + e.getMessage());
}The log shows Success: false and a REQUIRED_FIELD_MISSING status code. Passing false as the second argument lets the call return the error instead of throwing an exception. The error still means the save did not happen.
Why this step matters for your logic
Because validation runs first, a record that fails it never reaches your before-save automation. Code in the before-save flow or before trigger does not run for that record. If your trigger logging seems to be missing for some saves, check whether a required field or a layout rule rejected them first.
System validation also runs a second time. Salesforce repeats most of these checks after the before triggers finish, so a before trigger cannot leave a required field empty or break a data type. The repeat does not include the layout-specific rules. The next chapters cover that second pass together with custom validation rules.
Common mistakes
| Mistake | Result |
|---|---|
| Testing a trigger through Apex and assuming the UI behaves the same | The UI also enforces layout-required fields, so the UI save can fail earlier. |
| Expecting a before trigger to run for a record with an invalid format | The record is rejected before the trigger is reached. |
| Treating this check as a custom validation rule | System validation is built in and cannot be turned off or edited. |
Exercise
A user saves a new Contact in the UI and leaves out a field that is required only on the page layout. A before insert trigger would have filled that field. Does the trigger get the chance to fill it?
Show answer
No. The layout-required check is part of the first system validation, which runs before before-save flows and before triggers. The save fails with an error and the trigger never runs for that record. The same insert through Apex would skip the layout check.