Sub topic 4
API names, labels and the __c suffix
This chapter assumes you can open Setup and Object Manager, and that you know what an object and a field are. It covers how Salesforce names the same object in two ways, how to read the suffixes on those names, and which errors come from mixing them up.
Labels and API names
Every object and field has two names.
- The label is the text users see in the interface. It can contain spaces, can be translated, and can be renamed at any time.
- The API name is the identifier that code, queries, formulas and metadata files use. It is unique within its scope and has strict formatting rules.
When you create a custom object called "Invoice", Salesforce proposes the API name Invoice__c. The label is "Invoice" and the API name is Invoice__c. A user sees the label. A SOQL query or Apex class must use the API name.
Reading the suffixes
Standard objects and fields have API names without a suffix, such as Account or Industry. Custom items get a suffix that marks what they are.
| Suffix | Meaning | Example |
|---|---|---|
| `__c` | Custom object or custom field | `Invoice__c`, `Amount__c` |
| `__r` | Relationship name, used to reach a parent record's fields | `Account__r.Name` |
| `__mdt` | Custom metadata type | `Tax_Rate__mdt` |
| `__x` | External object | `Order__x` |
| `__b` | Big object | `Audit__b` |
| `__e` | Platform event | `Order_Event__e` |
Two details matter in practice.
- A custom field on a standard object also ends in
__c. A field you add to Account might beRegion__c, whileIndustryis a standard field with no suffix. __rdoes not name a field. It names the relationship that the lookup fieldAccount__ccreates. You useAccount__cto read or write the foreign key ID. You useAccount__r.Nameto read a field on the parent record.
Seeing both names in a query
The query below reads a custom object. Every identifier is an API name. The labels never appear.
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'Invoice__c is the object. Amount__c and Status__c are custom fields. Account__r.Name follows the Account__c lookup to the parent Account and returns its Name. Name itself has no suffix because it is the standard name field, which every object has.
Standard relationships follow a different pattern. Between standard objects, the child-to-parent name has no __r, and the parent-to-child name is a plural such as Contacts:
SELECT Id, Name, Industry,
(SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10How API names are created
You choose a label, and Salesforce builds a default API name from it. It replaces spaces with underscores and appends the suffix. The label "Invoice Number" gives Invoice_Number__c. You can edit the API name before you save the field.
The rules for the part before the suffix are these:
- It can use letters, numbers and underscores only.
- It must begin with a letter.
- It cannot end with an underscore or contain two underscores in a row.
Inspecting names in Apex
When you are unsure whether an object is custom, ask the schema. The describe result reports the API name and whether the object is custom.
Schema.DescribeSObjectResult accountDescribe = Account.SObjectType.getDescribe();
Schema.DescribeSObjectResult invoiceDescribe = Invoice__c.SObjectType.getDescribe();
System.debug(accountDescribe.getName() + ' custom? ' + accountDescribe.isCustom());
System.debug(invoiceDescribe.getName() + ' custom? ' + invoiceDescribe.isCustom());getName() returns the API name. getLabel() returns the label. The debug output shows Account custom? false and Invoice__c custom? true.
Common errors
- "No such column 'Amount' on entity 'Invoice__c'". You used the label or forgot the suffix. Use
Amount__c. - Using
Account__cwhere you need the parent's name.Account__cholds an ID. UseAccount__r.Nameto reach the name. - Using
__ron a standard relationship. The parent of a Contact isAccount, notAccount__r. - Assuming the label is unique. Two objects can share a label in different languages or after a rename. The API name is the reliable key.
- Deploying after a rename. The metadata file name is the API name, as in
Invoice__c.object-meta.xml. A renamed object is treated as a different component.
Exercise
A lookup field on Invoice_c has the label "Customer" and the API name Customerc, and it points to Account. Write the SOQL expression that returns the customer's Industry, and explain why Customer_c.Industry would fail.
Show answer
Use Customer_r.Industry, as in SELECT Name, Customerr.Industry FROM Invoicec. Customerc is the lookup field and holds only the parent record ID, so it has no sub-fields. The relationship name Customer_r is what lets a query reach fields on the parent Account.
Exercise
You rename the label of Amount_c to "Total" in Object Manager. Does an existing Apex class that references Amount_c still compile? What if you changed the API name instead?
Show answer
Yes, the class still compiles, because code uses the API name and only the label changed. If you changed the API name to Total__c, the class would no longer compile until you updated its references.