Sub topic 1
What a field is: standard fields, custom fields and API names
A field is one named, typed column of data on an object. An Account record stores its name, its phone number and its annual revenue in separate fields. Every other topic in this tutorial builds on how fields are defined and named, so this chapter covers that first.
Prerequisites
This chapter assumes you already know the following:
- Objects, records and fields as the basic parts of the Salesforce data model.
- How to open Setup and find an object in Object Manager.
- The difference between a standard object, such as Account, and a custom object, such as
Invoice__c.
You don't need to write Apex to follow the main text. The Apex and SOQL snippets are there to show how names behave in code.
What a field defines
A field definition is metadata. It describes the data that records of that object can hold. Every field has these properties:
- Label. The text users see on page layouts and reports. You can translate it and change it freely.
- API name. The identifier that code, formulas, integrations and metadata files use.
- Data type. Text, Number, Picklist, Lookup and so on. The type decides what the field can store and which side effects it has.
- Attributes. Settings such as required, unique and default value. A later chapter covers these.
The label is for people and the API name is for software. Keeping that split in mind prevents most naming mistakes.
Standard fields and custom fields
Salesforce ships every standard object with standard fields. You add custom fields yourself. The two kinds differ in what you can change.
| Aspect | Standard field | Custom field |
|---|---|---|
| Created by | Salesforce | You, an installed package, or a deployment |
| API name | No suffix, for example `AnnualRevenue` | Ends in a suffix, usually `__c` |
| Can you delete it? | No | Yes, though dependencies block deletion |
| Can you change the label? | Often yes | Yes |
| Can you change the type? | No | Often yes, with consequences covered in a later chapter |
| Counts against the custom field limit? | No | Yes. The limit depends on the edition |
Some standard fields are system-managed and read-only. Id, CreatedDate, CreatedById, LastModifiedDate, SystemModstamp and IsDeleted exist on nearly every object. You can read them, but you can't write most of them in normal operation.
Some standard fields offer limited customization. On many objects you can rename the label, so "Account Name" can become "Company". The API name stays Name. Code written against Name keeps working, even though users no longer see that word.
How API names are built
When you create a custom field, Salesforce suggests an API name from the label. A label of "Priority Score" gives Priority_Score__c. The part before the suffix is the developer name.
The rules for the developer name are:
- It can contain letters, numbers and underscores only.
- It must begin with a letter.
- It can't end with an underscore or contain two underscores in a row.
- It is limited to 40 characters. Because the suffix is added on top, the full API name is longer.
- It must be unique within the object. Names are not case sensitive, so
Region__candregion__care the same field.
Suffixes tell you what you are looking at:
| Pattern | Meaning |
|---|---|
| `Priority_Score__c` | A custom field |
| `Primary_Contact__c` | A custom relationship field, which holds the related record's Id |
| `Primary_Contact__r` | The relationship name, used to reach fields on the related record |
| `acme__Priority_Score__c` | A custom field from a managed package with the namespace `acme` |
On a standard relationship such as OwnerId, the relationship name is Owner with no suffix. For a custom lookup, the field ends in __c and the traversal name ends in __r. Mixing these up is a common source of query errors.
Label versus API name in practice
Because labels change often, never depend on them in code. The snippet below lists the fields on Account and shows both names side by side.
Map<String, Schema.SObjectField> fields = Schema.SObjectType.Account.fields.getMap();
for (String key : fields.keySet()) {
Schema.DescribeFieldResult d = fields.get(key).getDescribe();
if (d.isCustom()) {
System.debug(d.getName() + ' | ' + d.getLabel() + ' | ' + d.getType());
}
}The keys of getMap() are lowercase. Call getName() on the describe result to get the real API name with its original capitalization.
SOQL and formulas use API names too. Here the query reads one custom field and one field reached through a custom lookup.
SELECT Id, Name, Priority_Score__c, Primary_Contact__r.Name
FROM Account
WHERE Priority_Score__c > 50If an admin relabels "Priority Score" to "Urgency", this query still works. If someone changed the API name to Urgency__c, it would stop working.
Where the name appears in metadata
In the metadata format used by deployments, fullName holds the API name and label holds the display text. They are separate elements, which is why a label change is a safe edit and a fullName change is not.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Priority_Score__c</fullName>
<label>Priority Score</label>
<type>Number</type>
<precision>5</precision>
<scale>2</scale>
<required>false</required>
</CustomField>The type element is what the rest of this tutorial is about. Each chapter that follows explains one family of types and the elements that go with it.
How the pieces connect
The diagram shows which consumers read which name.
Common errors
- Using the label in code. A query that says
Priority Scorefails. Always use the API name. - Forgetting the suffix.
Priority_Scoreis notPriority_Score__c. The query returns an "No such column" error. - Confusing
__cand__r. Use__cto read the Id stored on the record. Use__rto reach fields on the related record. - Assuming a deleted field is gone at once. A deleted custom field goes to a recycle state for a period and still counts toward limits until it is purged.
- Renaming a field without searching for references. Check formulas, validation rules, flows, Apex and reports before editing an API name.
Exercise
A report uses the field labeled "Account Name". An admin relabels it "Company". Does an Apex query that selects Name from Account still work, and why?
Show answer
Yes. Apex and SOQL use the API name, which is Name and does not change when the label changes. Only what users see in layouts and reports changes.
Exercise
You need to reach the Name of the record in a custom lookup field Primary_Contact__c from a SOQL query on Account. Which expression do you write?
Show answer
Primary_Contact__r.Name. The __r relationship name traverses to the related record, while Primary_Contact__c holds only its Id.