Sub topic 2
Attributes that apply to many field types
This chapter assumes you can create a custom field in Object Manager and know the difference between a field's label and its API name. It covers the settings that appear on many field types: required, unique, external ID, and default value. The specific field types come in later chapters.
Why attributes matter
A field type decides what kind of data a field holds. An attribute decides what rules the platform applies to that data. Two Text fields can behave very differently if one is required and unique and the other is not.
Attributes are cheap to set when you create a field. Changing them later can fail or leave old data inconsistent. Decide on them when you design the field.
Required
A required field must have a value before a record can be saved. Salesforce offers two kinds of "required", and they are easy to confuse.
| Kind | Where you set it | Where it is enforced |
|---|---|---|
| Field-level required | Field definition ("Always require a value in this field in order to save a record") | UI, API, Apex DML, imports, Flow |
| Page layout required | Page layout field properties | Only users editing through that layout |
Only the field-level setting protects data that arrives from an integration or a data load. A layout-required field can still be blank when a record is created through the API.
Keep these points in mind:
- Not every field type can be required. Checkbox, formula, roll-up summary, and auto number fields cannot, because they always have a value or are calculated.
- A master-detail relationship field is always required, because a child record cannot exist without a parent.
- Making a field required does not change existing records. Records with a blank value stay blank until someone edits them, and then the save fails until the field is filled in. Clean up existing data first.
- If the rule depends on other fields (for example, "required when Stage is Closed Lost"), use a validation rule instead. A field-level required setting is unconditional.
Unique
A unique field rejects a value that already exists on another record of the same object. Uniqueness is available on Text, Email, Number, and Auto Number fields.
For Text fields, you choose how to compare values:
- Case-insensitive:
ABCandabcare duplicates. - Case-sensitive:
ABCandabcare different values.
Several details cause problems in practice:
- You cannot turn on uniqueness while duplicates exist. Find and fix the duplicates first.
- Blank values are not compared, so many records can have an empty value in a unique field. If you need every record to have a value, make the field required as well.
- A unique field is indexed automatically, which helps selective queries. The indexing chapter covers this.
- Uniqueness applies to one field on one object. For "unique per combination of two fields", Salesforce has no built-in setting. A common workaround is a text field that concatenates the two values, filled by a flow or trigger, and marked unique.
External ID
An external ID is a field that holds the record's identifier from another system, such as an ERP customer number. You can mark Text, Number, Email, and Auto Number fields as external IDs. An object can have up to 25 external ID fields.
External ID fields do two jobs:
- They are indexed, so lookups by that value are fast.
- They let you match records during upsert and data import without knowing the Salesforce record ID.
An upsert inserts a record if no match exists and updates it if one does. Here is an upsert keyed on a custom external ID field. The example assumes Account has a Text field named ERP_Id__c marked as an external ID.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme', ERP_Id__c = 'A-1001'),
new Account(Name = 'Globex', ERP_Id__c = 'A-1002')
};
Database.UpsertResult[] results =
Database.upsert(accounts, Account.ERP_Id__c, false);
for (Database.UpsertResult r : results) {
if (r.isSuccess()) {
System.debug((r.isCreated() ? 'Created ' : 'Updated ') + r.getId());
} else {
System.debug('Failed: ' + r.getErrors()[0].getMessage());
}
}Marking a field as an external ID does not make it unique. If two records share the same external ID value, an upsert that matches that value fails with a duplicate error. For integration keys, turn on both settings.
Here is the metadata for such a field:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>ERP_Id__c</fullName>
<label>ERP Id</label>
<type>Text</type>
<length>20</length>
<required>true</required>
<unique>true</unique>
<caseSensitive>false</caseSensitive>
<externalId>true</externalId>
</CustomField>Default value
A default value fills a field when a new record is created and no value is supplied. It is written as a formula, so it can be a constant or can depend on other data, such as the current user or today's date.
Examples of default value formulas:
- Text:
"Standard"(quotes are needed because the default is a formula) - Number:
10 - Date:
TODAY() + 30 - Checkbox: the setting is Checked or Unchecked, and every checkbox must have one.
Behaviors to remember:
- A default applies only on create. It does not fill blanks in existing records.
- A default is a starting value. Users and integrations can overwrite it, so it is not a rule. Use a validation rule if the value must be enforced.
- A default combined with a required field is a useful pattern: the field is never blank, and users only change it when needed.
- Picklists take their default from the value list, where you mark one value as the default.
Other attributes you will see
Several other settings appear on most fields. They do not change what data is accepted, but they affect how the field is used and maintained.
- Help text: the hover tooltip shown next to the label in the UI.
- Description: an internal note for administrators. Always fill it in, because it is the only documentation many fields will have.
- Track history: records changes to the field in the object's history, within the limit of fields tracked per object.
- Field-level security: controls which profiles and permission sets can see or edit the field. A later chapter covers it with encryption.
Choosing attributes for a field
The diagram shows a practical order for deciding which attributes to set.
Common mistakes
- Relying on layout-required to protect data that integrations also write.
- Marking a field as an external ID and assuming it is unique.
- Expecting a default value to update existing records.
- Turning on required or unique in production before checking existing data. Query for blanks and duplicates first.
- Forgetting that blank values pass a unique check.
Exercise
An integration loads Account records from an ERP system through the API. Each ERP customer has a unique customer number, and the same customer may be sent again later with changed data. Which attributes should you set on the Salesforce field that stores the customer number, and why would marking the field required only on the page layout be insufficient?
Show answer
Use a Text (or Number) field with External ID and Unique both turned on, and make it required at the field level. External ID lets the integration upsert on the customer number and makes the field indexed. Unique prevents two accounts sharing a number, which would make the upsert fail. Field-level required ensures records without a number cannot be created. Layout-required is enforced only in the UI for that layout, so API loads could still insert records with a blank customer number.