Sub topic 4
API names, labels and the __c suffix
This chapter assumes you can navigate Setup and Object Manager, understand tables, rows and foreign keys, and have seen a basic SOQL query. It covers how Salesforce names objects and fields, why the name you see on screen is not the name code uses, and which mistakes this causes.
Labels and API names are different things
Every object and field has two identifiers.
- The label is the text users read in the interface. You can change it at any time, and it can be translated.
- The API name is the identifier that code, queries, formulas, integrations and metadata files use. It is also called the developer name or the unique name.
When you create a custom object called "Invoice" in Object Manager, Salesforce proposes the API name Invoice__c. The part before the suffix is generated from the label, with spaces replaced by underscores. You can edit it before you save.
The label is for people and the API name is for software. Renaming a label never breaks a query. Changing an API name can break everything that refers to it.
| Property | Label | API name |
|---|---|---|
| Audience | End users | Code, queries, integrations |
| Example (custom object) | Invoice | `Invoice__c` |
| Example (custom field) | Amount | `Amount__c` |
| Can be translated | Yes | No |
| Safe to change later | Yes | Risky, and mostly blocked once referenced |
| Case sensitive | No | No, but use the exact spelling by convention |
Naming suffixes
Salesforce appends a suffix to the API names of things you create. The suffix tells you what kind of thing a name refers to. Standard objects and standard fields have no suffix.
| Suffix | Meaning | Example |
|---|---|---|
| (none) | Standard object or standard field | `Account`, `Industry` |
| `__c` | Custom object or custom field | `Invoice__c`, `Amount__c` |
| `__r` | Relationship name for a custom lookup or master-detail field | `Account__r` |
| `__mdt` | Custom metadata type | `Tax_Rate__mdt` |
| `__x` | External object | `Order__x` |
| `__b` | Big object | `Interaction__b` |
| `__e` | Platform event | `Order_Shipped__e` |
Custom objects, custom fields and custom relationships are the suffixes you will use most. The others show up in the later chapter on other custom entity types.
The `__c` suffix
__c stands for "custom". Both of these are custom:
Invoice__cis a custom object.Amount__cis a custom field. It can sit on a custom object or on a standard one. A custom field on Account might beAccount.Region__c.
You cannot tell from the suffix alone whether a __c field is on a standard or a custom object. You need to look at the object it belongs to.
The `__r` suffix
A lookup or master-detail field stores the Id of a parent record. In the example schema, Invoice__c.Account__c holds the Id of an Account. Salesforce also creates a second name for the same field, used to reach through to the parent record's own fields. For a custom relationship field, that name ends in __r.
| Name | What it gives you |
|---|---|
| `Account__c` | The Id of the parent Account (a value in the child row) |
| `Account__r` | The parent Account record, so you can read `Account__r.Name` |
For a standard relationship field, the pattern is different. The field Contact.AccountId has the relationship name Account, with no suffix. So Contact.Account.Name is valid, while Invoice__c.Account__r.Name is the custom equivalent.
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'This query reads Account__r.Name to get the parent account's name in the same result, without a second query. Using __r is the child-to-parent direction, and you can chain up to five levels, such as Account__r.Owner.Manager.Name.
Child relationship names
The parent-to-child direction uses a plural child relationship name. For standard relationships Salesforce already defines them, such as Contacts on Account. You use this name inside a subquery.
SELECT Id, Name, Industry,
(SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10For a custom relationship, the child relationship name is set when you create the field. By default it is the plural label of the child object, such as Invoices, and a subquery on a custom child then uses Invoices__r. If you ever see the error Didn't understand relationship, check the exact child relationship name in the field's definition.
How Salesforce builds an API name
Understanding the rules saves time when a name does not come out as you expect.
- Salesforce takes the label you enter.
- It replaces spaces and special characters with underscores.
- It removes repeated and trailing underscores.
- It appends the suffix.
The rules the platform enforces on the name portion are:
- It must begin with a letter.
- It can contain only letters, numbers and underscores.
- It cannot end with an underscore.
- It cannot contain two consecutive underscores, because a double underscore is reserved for suffixes and namespace prefixes.
- It must be unique within the object (for fields) or the org (for objects).
So the label "Total Amount (USD)" becomes something like Total_Amount_USD__c. If you try to type Total__Amount__c as the name part, Salesforce rejects it.
Namespace prefixes
Managed packages add a namespace prefix and a double underscore. A field Amount__c from a package with the namespace acme becomes acme__Amount__c. Code in your own org must use the full prefixed name to refer to it. Objects and fields you create directly in your org have no prefix.
Reserved words
You cannot use some words as API names, such as Id, Name on its own for a custom field, or other names that collide with system fields. Salesforce blocks them or asks for a different name.
Why the API name matters more than the label
Because the label can change, two kinds of confusion appear in real orgs.
- A field labelled "Customer Type" may have the API name
Segment__c, because it was renamed after creation. Searching code for the label finds nothing. - Two fields on the same object can have the same label but different API names. Labels do not have to be unique, API names do.
The practical rule is to read API names from the source of truth and never guess them from the screen. You can find them in Object Manager under Fields & Relationships, where each field lists both its Field Label and Field Name.
You can also confirm them in code. This Apex checks whether an object is custom and prints its API name.
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, so the debug lines print Account custom? false and Invoice__c custom? true. The describe result also offers getLabel() and getLabelPlural(), which return the label text for the running user's language. Using both methods side by side shows the label/API-name split directly.
Where API names appear
You use API names in many places, and each one is a place that breaks if the name changes.
- SOQL and SOSL queries.
- Apex code, including field references such as
inv.Amount__c. - Formula fields, validation rules and flows.
- Reports and list views, which store field references internally.
- REST and SOAP API calls, where JSON keys are API names.
- Metadata files in a Salesforce DX project.
The metadata file for a custom object is named after its API name, so Invoice__c lives in a file called Invoice__c.object-meta.xml. The label is stored inside the file.
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Invoice</label>
<pluralLabel>Invoices</pluralLabel>
<nameField>
<label>Invoice Number</label>
<type>Text</type>
</nameField>
<deploymentStatus>Deployed</deploymentStatus>
<sharingModel>ReadWrite</sharingModel>
</CustomObject>Here label and pluralLabel are display text. The API name comes from the file name, not from a tag inside the file. That is why renaming the file renames the object during deployment.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such column 'Amount' on entity 'Invoice__c'` | Missing `__c` on a custom field | Use `Amount__c` |
| `Didn't understand relationship 'Account' in FROM part of query` | Wrong child relationship name in a subquery | Look up the relationship name on the lookup field |
| `Invalid field Account__r.Nam` | Typo in a field reached through `__r` | Check the parent field's API name |
| Query works in a sandbox, fails in production | Field exists under a different API name in production | Compare metadata between orgs |
| Package field not found | Missing namespace prefix | Use `acme__Amount__c` |
| Cannot change API name | Field is referenced by other metadata | Create a new field, migrate data, retire the old one |
Changing API names safely
Treat an API name as a contract.
- Pick the name carefully when you create the object or field. Prefer short, descriptive, singular names such as
Invoice__c, and avoid abbreviations nobody will recognize. - Change labels freely to match business language.
- If a name is wrong, add a correctly named field, copy the data with a data load or Apex, update references, and then delete the old field.
- Remember that deleted custom fields stay in the recycle bin for 15 days and still count toward limits until they are erased.
Salesforce allows API name edits on some items, but it does not update every external reference, such as integrations outside the org. Even where the platform updates internal references like reports, code in other systems still points to the old name.
Using names in Apex DML
The same names apply when you create records. A field you set on an sObject must use its API name, and the relationship Id field uses the __c form.
Account acc = new Account(Name = 'Acme Corp');
insert acc;
Invoice__c inv = new Invoice__c(
Name = 'INV-0001',
Account__c = acc.Id,
Amount__c = 1200,
Status__c = 'Open'
);
insert inv;Set Account__c with an Id when you insert. You cannot assign through Account__r.Name, because __r is for reading the parent, not for setting the foreign key.
Exercise
A custom 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 name from an Invoicec query, and say why Customer_c.Name would fail.
Show answer
Use Customer_r.Name, as in SELECT Name, Customerr.Name FROM Invoicec. Customerc holds only the Id of the parent Account, which is a text value, so it has no Name field to read. The _r name is the relationship that follows the Id to the parent record, and fields on the parent are read through it.
Exercise
A teammate renames the label of Amount__c from "Amount" to "Total Due" and says all Apex and reports will now break. Is that correct? What would actually change the API name, and what should you do if you need a different one?
Show answer
No. Changing the label only changes what users see, so queries, Apex, formulas and integrations that use Amount__c keep working. The API name is separate and is not changed by editing the label. If you need a different API name, create a new field with the correct name, migrate the data, update every reference, and then delete the old field.