Sub topic 8
Required fields and how they interact with FLS
This chapter assumes you know how to set field Read and Edit access in Setup (see the earlier chapters on FLS and permission sets). It also assumes you can read basic Apex. It covers the three ways a field can be "required" and which of them field-level security can override.
Three meanings of "required"
Salesforce uses the word "required" for three different mechanisms. They sit at different layers, and only one of them is enforced everywhere.
| Mechanism | Where you set it | Enforced in | Interaction with FLS |
|---|---|---|---|
| Universally required | The **Required** checkbox on the field definition (or a platform-required field such as `Contact.LastName`) | UI, API, Apex, imports, Flow | FLS cannot hide or lock it. The field is always visible and editable. |
| Required on a page layout | The layout editor | Only the UI that uses that layout | FLS wins. If the user cannot edit the field, the layout requirement is ignored. |
| Validation rule | Validation rule on the object | Every save, from any entry point | Runs in system context, so it can check fields the user cannot see. |
The sections below take each one in turn.
Universally required fields
When you select Required on a custom field, the platform treats the field as part of the record's basic shape. A record cannot exist without a value in it. For this reason, FLS has no say over these fields.
- The field is always visible and editable for every profile and permission set.
- The FLS checkboxes for it are fixed in Setup, so you cannot clear Read or Edit.
- The field's value must be supplied by whatever creates the record: a UI form, an API call, a Flow, or Apex.
Standard fields can be required in the same way. Account.Name, Contact.LastName, and Opportunity.StageName are examples. The same applies to master-detail relationship fields, which must point to a parent record.
The check does not depend on user mode. A user-mode insert that omits a required value fails because the value is missing. It does not fail because of a permission problem.
Contact c = new Contact(FirstName = 'Ada'); // LastName is required
try {
Database.insert(c, AccessLevel.USER_MODE);
} catch (DmlException e) {
System.debug(e.getDmlStatusCode(0)); // REQUIRED_FIELD_MISSING
System.debug(e.getDmlFieldNames(0)); // (LastName)
}Consequences for design
Making a field universally required exposes it to every user who can create the record. This matters when the field holds sensitive data. Suppose you mark Internal_Notes__c as required and it contains information that only some teams should see. Every user who can create the object will see the field and will have to fill it in. You cannot fix this with FLS later.
If a field must be sensitive and also mandatory for some cases, use one of these approaches instead:
- Leave the field optional and enforce the rule with a validation rule that applies only to the right profiles or roles.
- Set the field through automation that runs in system context, such as a before-save Flow or a trigger, so that the user never handles the value.
- Move the sensitive data to a separate object that has its own object permissions.
Making an existing field required also has a side effect on data. You cannot mark a field required if existing records have blank values in it, unless you supply a default or populate the field first.
Required on the page layout only
A field can be marked required only in the page layout. This is a UI rule. The API, Apex, Flow, and data imports do not apply it. It is also a weak rule, because FLS takes priority over it.
The behavior depends on the user's access:
- The user has Edit access to the field. The layout shows the red required marker and the UI blocks the save until the field has a value.
- The user has Read-only access. The field displays as read-only, so the user cannot fill it in. The requirement does not block the save.
- The user has no access. The field is not shown at all, and the save works.
This is a common source of confusion. An admin marks a field as required on the layout and then finds blank values in the data. The cause is usually one of three things: users without Edit access, records created through the API or an import, or a different layout assigned to some profiles.
A layout requirement is a convenience for data entry. It is not a data quality guarantee. If you need a guarantee, use the universal Required setting or a validation rule.
Validation rules and hidden fields
Validation rules are formulas that run when a record is saved. They run in system context. A rule can therefore read a field that the saving user cannot see, and it evaluates the real value.
This produces a failure mode that is hard to debug. A rule can block a user because of a field that user cannot see or edit, and the error message may point to something they cannot act on.
The following rule requires notes when a case is closed:
<?xml version="1.0" encoding="UTF-8"?>
<ValidationRule xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Require_Notes_When_Closed</fullName>
<active>true</active>
<description>Closed cases must have internal notes.</description>
<errorConditionFormula>AND(ISPICKVAL(Status, "Closed"), ISBLANK(Internal_Notes__c))</errorConditionFormula>
<errorMessage>Add internal notes before closing the case.</errorMessage>
</ValidationRule>Suppose Internal_Notes__c is hidden from a group of users through FLS. Those users cannot close any case, because the rule fails and they cannot add notes. The same happens if the field is read-only for them.
There are three ways to handle this:
- Add a condition to the formula that skips the check for users who cannot supply the value, for example by testing
$Profile.Nameor$Permission.Can_Edit_Internal_Notes. Custom permissions are the better choice, because they do not break when a profile is renamed. - Give the affected users Edit access to the field.
- Write the error message so that it names the team or process that can resolve it.
You can set the error location on the rule to a specific field. If the user cannot see that field, the message appears at the top of the page, so write messages that stand on their own.
What this means for deployments
Metadata deployments treat universally required fields as special. If you include a fieldPermissions entry for a required field in a permission set or profile, the deployment fails with an error similar to "You cannot deploy to a required field". Remove those entries. The platform already grants the access.
When you deploy a new required field together with the permission sets that mention other fields, keep the required field's entry out of the permission set XML. Fields that are only required on a layout do need fieldPermissions entries, because FLS still controls them.
Checklist for a new required field
- Decide whether the value must exist for every record, from every source. If yes, use the universal Required setting. If not, use a validation rule.
- Check whether the field holds sensitive data. If it does, avoid the universal setting.
- Look for validation rules that read fields hidden from some users.
- Remove required fields from
fieldPermissionsin your deployment sources. - Test creation through the UI, the API, and Apex in user mode for at least one restricted user.
Exercise
A field Region__c is marked required only on the Sales layout. A user whose profile has no access to Region__c creates a record in the UI. What happens, and why? What would change if the field were universally required?
Show answer
The save succeeds. The field is not displayed to the user because FLS hides it, so the layout requirement cannot apply. If the field were universally required, FLS could not hide it. Every user would see it and have to supply a value, and any save without one would fail with REQUIREDFIELDMISSING, from the UI, the API, and Apex.
Exercise
A validation rule checks ISBLANK(Internal_Notes__c) when a case is closed. Support agents cannot see Internal_Notes__c. What happens when they close a case, and what is one way to fix it without exposing the field?
Show answer
The rule runs in system context and sees that the field is blank, so it blocks the save, and agents cannot resolve the error. One fix is to skip the rule for these users with a custom permission check in the formula, for example AND(..., NOT($Permission.Skip_Notes_Check)). Another is to fill in the notes through automation that runs in system context.