Sub topic 1
What a field is: standard fields, custom fields and API names
Prerequisites
This chapter assumes you already know the following:
- The basic Salesforce data model: objects hold records, and records hold values in fields.
- How to open Setup and Object Manager.
- The difference between a standard object, such as Account, and a custom object.
What a field is
A field is one named column on an object. It has a data type, and the type decides what values it accepts, how they are stored and how they appear in the UI, reports and code. Every record of an object has a slot for each field, even when the slot is empty.
Fields come in two kinds:
- Standard fields ship with Salesforce. Examples are Name, Industry and AnnualRevenue on Account. You can't delete most of them, and you can change only some of their properties, such as the label or help text.
- Custom fields are fields you create. You can change or delete them, subject to dependencies. Salesforce gives them an API name that ends in
__c.
Label, field name and API name
Each field has three names, and mixing them up causes common errors.
| Name | Where you see it | Example | Can it change? |
|---|---|---|---|
| Label | Page layouts, reports | Priority Score | Yes, at any time |
| Field Name | Setup, when you create the field | Priority_Score | Only by editing the field, and it affects code |
| API name | Apex, SOQL, formulas, metadata, integrations | Priority_Score__c | Treat it as permanent |
Salesforce suggests a field name from the label, replacing spaces with underscores. The API name is the field name plus the __c suffix. Standard fields have no suffix, so the API name of Annual Revenue is AnnualRevenue.
Seeing the type in code
Because code uses API names, you can ask Apex what type a field has. The result is the declared type, whatever the UI shows.
Schema.DescribeFieldResult revenue = Account.AnnualRevenue.getDescribe();
System.debug(revenue.getType()); // CURRENCY
Schema.DescribeFieldResult accountType = Account.Type.getDescribe();
System.debug(accountType.getType()); // PICKLISTBoth lines use the API name. Account.AnnualRevenue is a standard field, and a custom field would be written Account.Priority_Score__c.
How a field is defined in metadata
A custom field is stored as metadata. The following definition describes a Number field. Notice that the API name appears in fullName, and the label is a separate element.
<?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 the choice this tutorial is about. The later chapters cover what each type stores and what limits come with it.
Why this matters for type choice
The type is set when you create the field. Changing it later is possible only in some cases and can lose data or break dependent items. Naming has the same property. Decide the type and the API name together, before other items start to reference the field.
Exercise
A teammate renames the label of PriorityScorec to "Risk Score" and asks whether the Apex class that reads PriorityScore__c will break. What is your answer, and what would change the answer?
Show answer
It will not break, because Apex refers to the API name and the label change leaves PriorityScore_c unchanged. The answer would change if someone also changed the API name, because every reference to the old API name would then fail.