Sub topic 5
Standard and custom fields
A field is a single column of data on an object. Every record stores one value for each field on its object. This chapter covers what separates standard fields from custom ones, the main field types, the settings that control data quality, and the mistakes people commonly make. It assumes you know what objects and records are, can navigate Setup and Object Manager, and understand relational data (columns, foreign keys). The SOQL and Apex samples are short and need only basic SQL-style reading skills.
Standard fields and custom fields
Standard fields ship with the platform. Custom fields are ones you add. The two differ in naming, in what you can change, and in where they come from.
| Aspect | Standard field | Custom field |
|---|---|---|
| API name | No suffix, such as `Industry` or `CloseDate` | Ends in `__c`, such as `Amount__c` |
| Created by | Salesforce | You, a package, or a deployment |
| Can be deleted | No | Yes (it goes to a deleted-fields list first) |
| Label, help text | Editable | Editable |
| Type | Fixed | Changeable within limits |
| Appears on | Standard and custom objects | Standard and custom objects |
Custom objects also receive standard fields. When you create Invoice__c, Salesforce adds Id, Name, OwnerId, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp and IsDeleted for you. You can add custom fields to standard objects too. Account with a custom Region__c field is common. A custom field on a standard object is still custom, so it carries the __c suffix.
Several standard fields can be tailored even though they cannot be removed. You can change the label of Industry and edit its picklist values. You can make a standard field required on a layout, or hide it from layouts and from profiles. Some standard fields, such as Name on Contact, are compound fields. Compound fields group several parts, such as FirstName and LastName, into one display value.
The field types you will use
Choosing a type is the most consequential decision when you create a field. Changing it later can truncate or lose data.
| Type | Stores | Notes |
|---|---|---|
| Text | Up to 255 characters | Use for short identifiers and names |
| Text Area (Long) | Up to 131,072 characters | Cannot be used in filters or sorting in the same way as text |
| Number | Numeric value | You set the total digits and the decimal places |
| Currency | Money amount | Honors the org currency, and multi-currency if enabled |
| Percent | Numeric value shown with % | Stored as a number, displayed as a percentage |
| Date, Date/Time | Calendar values | Date/Time is stored in UTC and shown in the user's time zone |
| Checkbox | True or false | Always has a value, defaulting to false |
| Picklist | One value from a list | Can be restricted to the defined values |
| Multi-Select Picklist | Several values from a list | Stored as a semicolon-separated string |
| Email, Phone, URL | Formatted text | Email and URL are format-validated |
| Auto Number | Generated sequence | Read-only, with a format such as `INV-{0000}` |
| Formula | Calculated value | Read-only, computed when read |
| Roll-up Summary | Aggregate of child records | Needs a master-detail relationship |
| Lookup, Master-Detail | Reference to another record | Covered in the relationships chapter |
Formula and roll-up fields
A formula field is not stored. Salesforce evaluates it every time the record is read. This keeps it always current, but it means you cannot write a value to it in a form, an import or Apex. It can reference fields on the same record and on parent records through relationships. For example, this formula returns the number of days since an invoice was created:
TODAY() - DATEVALUE(CreatedDate)CreatedDate is a Date/Time, so DATEVALUE converts it to a date before the subtraction. If you skip that, the formula fails to save because the types do not match.
A roll-up summary field lives on the master side of a master-detail relationship. It calculates a COUNT, SUM, MIN or MAX over the detail records. A lookup relationship cannot host one. If you need an aggregate over a lookup, the usual choices are a flow, an Apex trigger or a declarative rollup tool.
Settings that protect data quality
Each field has settings beyond its type. Most of them change how data is validated, so set them when you create the field. Adding them later can fail if existing data already violates them.
- Required. A field marked required on its definition is enforced everywhere, including the API, imports and Apex. A field marked required only on a page layout is enforced in the UI and nowhere else.
- Unique. Rejects duplicate values. You can choose whether comparison is case-sensitive.
- External ID. Marks a field as a key from another system. It is indexed and can be used in
upsertoperations. - Default value. A literal or a formula applied when a record is created.
- Help text. A tooltip shown beside the field label. Good help text prevents many questions.
- Field history tracking. Records old and new values. Each object can track a limited number of fields (20), so choose carefully.
- Restricted picklist. Rejects values that are not in the list, even when they arrive through the API.
Here is how those settings look in source form. In a Salesforce DX project, a custom field is a file at force-app/main/default/objects/Invoice__c/fields/Amount__c.field-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount__c</fullName>
<label>Amount</label>
<type>Number</type>
<precision>18</precision>
<scale>2</scale>
<required>false</required>
<description>Invoice total before tax.</description>
<inlineHelpText>Enter the total before tax.</inlineHelpText>
</CustomField>precision is the total number of digits and scale is how many of them sit after the decimal point. A precision of 18 with a scale of 2 allows 16 digits before the decimal point and 2 after it.
Labels, API names and field naming
Every field has a label, shown to users, and an API name, used by code, formulas, reports and integrations. When you type a label in Object Manager, Salesforce proposes an API name by replacing spaces with underscores and adding __c. Review that proposal. The label "Amount" can be edited freely later, but the API name is hard to change once something references it.
A relationship field has two names. A lookup field Account__c holds the ID. To read the parent's fields in SOQL, you use the relationship name, which swaps __c for __r, as in Account__r.Name.
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'Standard relationships follow a similar pattern but without suffixes. Contact.AccountId is the ID, and Contact.Account.Name reaches the parent name.
Inspecting fields in code
Describe calls let you list the fields an object has and see which are custom. This is useful for debugging a deployment or auditing an object.
Map<String, Schema.SObjectField> fields =
Invoice__c.SObjectType.getDescribe().fields.getMap();
for (String key : fields.keySet()) {
Schema.DescribeFieldResult d = fields.get(key).getDescribe();
System.debug(d.getName() + ' | ' + d.getType() + ' | custom=' + d.isCustom());
}The map keys are lowercase, so use getName() when you need the correctly cased API name. Run the code in the Developer Console's Execute Anonymous window and open the debug log to read the output. You should see Name, OwnerId and the other standard fields with custom=false, and Amount__c with custom=true.
Writing to a field uses the same names:
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;If you try to assign a value to a formula or auto-number field in this code, the compiler rejects it because those fields are read-only.
Common errors and how to avoid them
- Changing a field's type and losing data. Switching from Text to Number can wipe values that do not parse. Switching from Text to Picklist keeps the values but leaves them outside the picklist list. Test on a sandbox copy first.
- Assuming layout-required means required. A field required only on a layout lets bad data in through imports, flows and the API. Use the field definition's required setting, or a validation rule, when the rule must always hold.
- Referencing a field that users cannot see. Field-level security is separate from the layout. A field on the layout is invisible to a user whose profile or permission sets do not grant read access. Apex running in system mode may read it anyway, which can hide the problem until a user reports it.
- Forgetting that formula fields are read-only. You cannot import into them, update them with DML, or edit them in the UI.
- Using a multi-select picklist as a filter or key. Values are stored in one string, so filtering needs
INCLUDESorEXCLUDES, and reporting is clumsy. If you need to query individual choices often, a junction object is usually the better model. - Hitting the field limit. Each object has a cap on custom fields that depends on the edition, and the cap is counted against deleted fields too until they are purged. Check the limits for your edition before designing a very wide object.
- Renaming an API name in production. Formulas, flows, Apex and reports that use the old name break. Prefer adding a new field and migrating the data.
- Deleting a field in use. Salesforce blocks deletion if something references the field, such as a formula, a flow or a report. A deleted field stays recoverable in Setup for 15 days, and then it is permanently removed.
Summary of the working model
Think of an object as a table and a field as a typed column. Standard fields come with the platform and keep their API names without a suffix. Custom fields have the __c suffix, and you control their type, validation and visibility. Type and required-ness decide what the data layer will accept. Layouts and field-level security decide what users can see and edit. When something looks wrong, ask first whether the problem is the data rule, the layout or the security.
Exercise
An Invoice__c object has a lookup field Account__c. You want a field that always shows the invoice's age in days and is never edited by users. Which field type should you use, and how do you read the parent account's name in the same SOQL query that selects Amount__c?
Show answer
Use a Formula field (for example TODAY() - DATEVALUE(CreatedDate)), because it is calculated on read and is read-only. In SOQL, select Account__r.Name alongside Amount__c, for example SELECT Amount__c, Account__r.Name FROM Invoice__c. The __r name replaces the __c of the lookup field.
Exercise
Your team marks Status__c as required only on the Invoice page layout. A nightly integration then inserts invoices with no status. Why does this succeed, and what are two ways to prevent it?
Show answer
Layout-required is enforced only in the user interface, so the API accepts records without a value. To prevent it, mark the field required in its definition, or add a validation rule that checks for a blank status. A default value on the field also avoids empty statuses.