Sub topic 3
System validation before triggers run
This chapter covers step 3 of the order of execution: the first validation pass, which runs before any automation. It assumes you can write a basic Apex trigger and know how validation rules work. It also assumes you know what a DML operation is. The previous chapters covered how a save is one transaction and how Salesforce loads the original record and overwrites it with the new values.
What system validation checks
System validation is the built-in checking Salesforce does without any configuration from you. Validation rules are not part of it. Those are custom validation and run later.
When a save comes from a standard user interface edit page, Salesforce checks the incoming record in step 3. It checks three kinds of rules:
- Layout-specific rules. A field marked required on the page layout must have a value.
- Field-level rules. Values must have a valid format for their type, such as a date, a number, or an email address. A restricted picklist must receive one of its defined values.
- Maximum field length. A text value must fit the field's defined length.
If any check fails, the save stops here. The user sees an error, and no flow, trigger, or custom validation rule has run.
The source of the save changes what runs
The step 3 checks run only when the request comes from a standard UI edit page. Saves from an Apex DML statement, the SOAP API, or the REST API skip layout-specific checks. Page layouts do not exist for those callers.
| Save source | Layout-required fields checked in step 3 | Later system validation (after before triggers) |
|---|---|---|
| Standard UI edit page | Yes | Yes |
| Apex DML | No | Yes |
| SOAP or REST API | No | Yes |
The same record can therefore pass through Apex and fail through the UI. A field that is required only on the layout can be blank in a record created by an integration. Do not assume every record in your org met the layout rules.
Why the order matters to your trigger
Salesforce runs most system validation twice. The first pass is step 3, before the before-save flows and before triggers. The second pass comes after before triggers, in the same step as custom validation rules. It repeats the checks for required fields, but it does not repeat the layout-specific rules.
This produces two behaviors that surprise many developers.
- A UI save can reject a record before your before trigger can repair it. If a field is required on the layout and the user leaves it empty, step 3 blocks the save. Your trigger never runs.
- An Apex save can pass a missing required field to your before trigger. A field required at the field level does not have to have a value yet. The trigger can fill it in, and the second validation pass then finds it populated.
The diagram shows where the two passes sit.
Example: filling a required field in a before trigger
Account Name is required at the field level. A before insert trigger can supply a default when an Apex or API caller leaves it empty. The second validation pass runs after the trigger, so the insert succeeds.
trigger AccountDefaultName on Account (before insert) {
for (Account a : Trigger.new) {
if (String.isBlank(a.Name)) {
a.Name = 'Unnamed account ' + String.valueOf(Datetime.now().getTime());
}
}
}You can check it by running this in the Developer Console's anonymous Apex window:
Account a = new Account();
insert a;
Account saved = [SELECT Name FROM Account WHERE Id = :a.Id];
System.debug(saved.Name);The same trigger does not help a user who creates an Account from the standard UI with the Name field empty. Step 3 rejects that save before the trigger runs.
Errors you will see
- Required field errors in the UI that your trigger seems to ignore. The layout check ran first. Make the field optional on the layout, or set a default value on the field definition, which is applied when the record loads.
- A before trigger fix that works in tests but fails for users. Tests use Apex DML, which skips layout rules. Test UI-specific behavior manually or through a flow that mimics the user's input.
- Truncation worries. Salesforce does not silently shorten text. A value over the maximum length causes a
STRING_TOO_LONGerror in DML or a field error in the UI. Trim long values in the before trigger if you want to avoid the error. - Restricted picklist failures. An invalid value raises
INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST. A before trigger runs after the first check on UI saves, but it can still correct the value for API saves.
Practical rules
- Assume a UI save can fail before any of your automation runs.
- When you debug a missing trigger log, check whether the save failed at step 3.
- Keep required-field logic in one place, either the field definition, the layout, or the trigger. Do not split it.
Exercise
A user creates a Contact from the standard UI and leaves a layout-required field blank. A before insert trigger on Contact would fill that field with a default. Does the trigger run, and why? What would happen if an Apex insert left the same field blank?
Show answer
The trigger does not run for the UI save. Step 3 system validation checks layout-required fields first and stops the save with an error. The Apex insert skips layout-specific checks, so the before trigger runs and can fill the field. The second validation pass then checks only field-level requirements, so the insert can succeed.