Sub topic 5
Standard and custom fields
A field is a column on an object. Each record stores one value per field. This chapter covers how standard and custom fields differ, which field types to pick, and the errors that most often come up. It assumes you know how to open Setup and Object Manager, and that you understand tables, rows, columns and foreign keys.
Standard fields versus custom fields
Standard fields ship with the platform. Name, Industry and Phone on Account are standard fields. Every object also has system fields such as Id, CreatedDate, CreatedById, LastModifiedDate and OwnerId (on objects that have owners). Standard fields have no suffix in their API name.
Custom fields are fields you add. Their API names end in __c, for example Amount__c. You can add custom fields to standard objects and to custom objects.
| Aspect | Standard field | Custom field |
|---|---|---|
| API name | `Industry` | `Status__c` |
| Can be deleted | Usually no | Yes |
| Created by | Salesforce | You |
| Available on | Standard objects | Standard and custom objects |
Some standard fields can be hidden from layouts or restricted by security, but you cannot remove them from the object.
Common field types
Choosing the right type matters because changing it later can lose data or be blocked.
- Text and Text Area: short strings, up to 255 characters for Text.
- Number, Currency and Percent: numeric values with a set number of digits and decimal places.
- Date and Date/Time: calendar values.
- Checkbox: true or false.
- Picklist and Multi-Select Picklist: values from a defined list.
- Formula: a value calculated when the record is read. It is read-only and is not stored.
- Roll-Up Summary: an aggregate (count, sum, min, max) of child records. It exists only on the master side of a master-detail relationship.
- Lookup and Master-Detail: references to another object. The next chapter covers these in detail.
Creating a custom field
- In Setup, open Object Manager and select the object.
- Choose Fields & Relationships, then New.
- Pick the data type and select Next.
- Enter the label. Salesforce proposes an API name from the label, and you can edit it before saving.
- Set options such as length, required, unique and default value.
- Choose which profiles can see and edit the field.
- Choose which page layouts show it, then save.
Reading fields in code
Fields are available to queries and Apex only if the running user can see them. This SOQL query mixes standard and custom fields on the Invoice__c object:
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'Name, Amount__c and Status__c are fields on the invoice. Account__r.Name follows the lookup field Account__c to the parent Account. The __r suffix is used for the relationship, while __c names the field that holds the Id.
Common errors
- No such column. The API name is misspelled or lacks
__c. Check the exact name in Object Manager. - Field not visible. Field-level security hides the field from the user's profile or permission sets. The query then fails, or the field is missing from results in some interfaces.
- Cannot change the type. Some changes are blocked when the field is used in formulas, reports or Apex, or when existing data does not fit the new type.
- Required field fails on insert. Fields marked required, and the
Namefield on most objects, must have a value when you create a record. - Picklist value rejected. Restricted picklists accept only defined values.
- Deleting a field. A deleted custom field goes to a recycle state for 15 days, but you cannot delete it while other metadata references it.
Exercise
A developer's SOQL query on Invoice__c fails with "No such column 'Amount'". The field's label is "Amount" and it is a custom Number field. What is the likely cause and the fix?
Show answer
The query uses the label instead of the API name. Custom fields end in __c, so the query should select Amount__c. Check the API name in Object Manager under Fields & Relationships.
Exercise
A field shows a calculated total of related child records and cannot be edited. Which field type is it likely to be, and which relationship type does it require?
Show answer
It is a Roll-Up Summary field. It requires a master-detail relationship, and it lives on the master (parent) object.