Sub topic 2
Attributes that apply to many field types
This chapter assumes you know what objects, records and fields are, and that you can find a field in Object Manager. It covers the settings that appear on many field types: required, unique, external ID and default value.
Required
A required field must have a value before a record can be saved. Salesforce enforces this in the UI, in the API and in Apex DML (data manipulation language, such as insert and update).
There are two related controls that people confuse:
- Required on the field definition. It applies everywhere, including data loads and API calls.
- Required on a page layout. It applies only to users who see that layout. An integration that writes through the API can skip it.
Making an existing field required does not change old records. Records that are already blank stay blank, but a user who edits one of them must fill in the field before saving.
Checkbox fields cannot be required, because false is a valid value. Master-detail relationship fields are always required.
Unique
A unique field rejects a duplicate value on another record of the same object. You choose whether the comparison is case-sensitive or case-insensitive. Text, Email and Number fields can be unique.
Two cautions:
- You cannot turn on Unique if duplicates already exist. Clean the data first.
- Blank values are not treated as duplicates of each other, so many records can have an empty value. Add Required if every record needs one.
External ID
An external ID marks a field as the key from another system, such as an ERP order number. It is available on Text, Number, Email and Auto Number fields.
It gives you two things:
- Upsert. Data tools and Apex can match on the field. If a record with that value exists, it is updated. If not, a new record is created.
- Indexing. Salesforce indexes the field, which makes filters on it faster. A later chapter covers indexes.
External ID alone does not prevent duplicates. If the other system's key must be unique here too, turn on Unique as well.
Account acct = new Account(
Name = 'Acme',
ERP_Id__c = 'A-1001'
);
// Updates the Account with ERP_Id__c = 'A-1001', or inserts one if none exists.
upsert acct ERP_Id__c;Default value
A default value pre-fills a field when a user starts a new record in the UI. It is a formula, so it can be a constant such as "Prospect" or TRUE, or it can use values like TODAY() or $User.Id.
The default is not applied to records already in the database. It also does not guarantee a value, because the user can clear it. Records created through the API do not receive the default in every case, so do not use it as a substitute for Required.
| Attribute | Enforced on | Affects existing records | Typical use |
|---|---|---|---|
| Required | UI, API, Apex | No | Data that must always exist |
| Unique | UI, API, Apex | Blocked if duplicates exist | Natural keys |
| External ID | Upsert matching | No | Keys from other systems |
| Default value | New records in the UI | No | Saving typing |
How the attributes combine
Exercise
An integration loads orders from an ERP system by order number. The same order must never appear twice, and a re-sent order should update the existing record. Which attributes do you set on the order number field?
Show answer
Use a Text field (or Number) with External ID on, so upsert can match on it, and Unique on, so duplicates are rejected. Add Required if every order must have a number. External ID alone would not block duplicates.
Exercise
You make Region_c required on a field definition. What happens to 10,000 existing records where Region_c is blank?
Show answer
They stay blank. The requirement is checked only when a record is saved, so a user editing one of those records must enter a region before saving.