Sub topic 3
Creating custom objects in Object Manager
This chapter assumes you can navigate Setup, understand tables, rows, columns and foreign keys, and have read the earlier chapters on how Salesforce stores data and on standard objects. You will create a custom object, learn what each setting does, and see the errors people most often hit.
What a custom object is
A custom object is a table you define for data that the standard objects do not cover. Each custom object has its own fields, page layouts, security settings and API name. Salesforce gives it the same platform features that standard objects have, such as reports, list views, sharing and the API.
The running example in this tutorial is Invoice__c. It stores invoices, and each invoice points at an Account.
Where you create it
Object Manager is the Setup area that lists every object in the org, standard and custom. You open it from Setup by searching for "Object Manager". To start, select Create, then Custom Object.
The creation form
The form asks for the following values. Several are hard to change later, so decide them carefully.
| Setting | Meaning | Can you change it later? |
|---|---|---|
| Label | Singular name shown in the UI, such as "Invoice" | Yes |
| Plural Label | Name used in tabs and list views, such as "Invoices" | Yes |
| Object Name | The API name without the suffix. Salesforce adds `__c`. | Technically yes, but it breaks code that references the old name |
| Record Name | The label of the standard Name field, such as "Invoice Number" | Label yes. Data type no, with limits noted below |
| Data Type of Name | Text or Auto Number | Switching is allowed with restrictions |
| Description | Internal note for admins | Yes |
The Name field
Every custom object has a Name field. It is the human-readable identifier that appears in lookups, list views and search results. You choose one of two types:
- Text: users type the value. Names such as
INV-0001can be duplicated unless you add your own validation. - Auto Number: Salesforce generates the value from a display format, for example
INV-{0000}, and a starting number. Users cannot edit it.
The Name field is not the record's unique key. The 15 or 18 character record Id is. Two records can have the same Name.
Optional features
The form includes checkboxes that turn on platform features. Some cannot be turned off once the object holds data or is referenced by other features.
- Allow Reports: lets users build reports on the object. Reporting is required before you can add the object to custom report types.
- Allow Activities: lets users log tasks and events against the records.
- Track Field History: records changes to selected fields. You pick the fields later.
- Allow Sharing, Allow Bulk API Access and Allow Streaming API Access: control which APIs and sharing features the object supports.
- Allow Search: includes the object in global search results.
- Launch the New Custom Tab Wizard: creates a tab so users can reach the object from the navigation bar.
Deployment status
An object has one of two statuses.
- In Development: the object is hidden from regular users. Use this while you are building.
- Deployed: the object is available to users who have permission.
Deployed is the normal choice for any org where you can control access with permissions. The metadata example at the end of the chapter uses Deployed.
What Salesforce creates for you
Saving the object does more than create a table. The platform adds several things automatically.
- A Name field of the type you chose.
- System fields:
Id,OwnerId,CreatedById,CreatedDate,LastModifiedById,LastModifiedDateandSystemModstamp.OwnerIdexists only when the object is not the detail side of a master-detail relationship. - A default page layout named after the object.
- A default compact layout, which controls the highlights shown in the record header and mobile views.
- A default list view, All.
- The API name, built from the Object Name you entered plus
__c.
The object has no user permissions yet. Nobody except admins with "Modify All Data" can see it until you grant access.
Creating the object in practice
For Invoice__c, you would enter these values:
- Label:
Invoice - Plural Label:
Invoices - Object Name:
Invoice(the API name becomesInvoice__c) - Record Name:
Invoice Number - Data Type: Auto Number, display format
INV-{0000}, starting number1 - Allow Reports, Allow Activities, Track Field History, Allow Search: selected
- Deployment Status: Deployed
After saving, Salesforce opens the object's detail page. The left menu lists the areas you configure next: Fields & Relationships, Page Layouts, Record Types, Validation Rules, Triggers and others.
Adding fields
An object with only a Name is not useful. Open Fields & Relationships and select New. For Invoice__c, add:
| Field label | API name | Type |
|---|---|---|
| Account | `Account__c` | Lookup to Account |
| Amount | `Amount__c` | Currency |
| Status | `Status__c` | Picklist with values Open, Paid, Void |
The field wizard asks for the type, label, API name, and then field-level security and page layout placement. The next chapters cover field types and relationships in detail. For now, note that the wizard's security step decides which profiles can see the field, so do not skip it.
Confirming the result in code
You can check from Apex that the platform treats the object as custom. The schema describe methods return metadata about any object.
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());Run this in the Developer Console under Debug, then Open Execute Anonymous Apex, and check the debug log. The expected output has Account custom? false and Invoice__c custom? true.
If the second line fails to compile with "Invalid type: Invoice__c", the object does not exist in that org, or the API name differs from what you typed.
Granting access
A new object is invisible to most users. The recommended approach is a permission set, because it adds access without changing profiles.
- In Setup, open Permission Sets and create a set such as
Invoice Access. - Open Object Settings, then select
Invoices. - Choose the object permissions (Read, Create, Edit, Delete) and the field permissions for each field.
- Assign the permission set to users.
Object permission and field permission are separate. A user with Read on the object but no access to Amount__c sees the record without that field. Security is covered in a later chapter.
If you also want a navigation entry, add the tab or add the object to a Lightning app through App Manager. Without a tab or app entry, users can still reach records through search, related lists and direct links.
Defining the object as metadata
Everything you configured in the UI can also be expressed as a metadata file. In a Salesforce DX project, the object lives in a file named Invoice__c.object-meta.xml.
<?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>This minimal definition uses a Text name. To get the Auto Number behavior described above, the nameField would use <type>AutoNumber</type> and a <displayFormat> such as INV-{0000}. The sharingModel value ReadWrite is the organization-wide default and means every user with object access can read and edit every record. The last chapter of this tutorial covers deployment between orgs.
Common errors and edge cases
- Wrong API name. The Object Name becomes the API name, so a typo such as
Inviocestays in code and integrations. Renaming later means updating every Apex class, flow, report and integration that references it. - Duplicate names. An object name must be unique within the org, and reserved words are rejected. If you install a managed package, its objects carry a namespace prefix, such as
acme__Invoice__c. - Name field confusion. Users sometimes expect Name to be unique. It is not. Add a unique custom field or validation rule if you need uniqueness.
- Master-detail added late. You can add a master-detail field to an object that already has data only when every existing record has a value for the parent. Otherwise the change fails or you must populate a lookup first.
- Deleting an object. Deleting moves it to the Recycle Bin for a limited period, and its data goes with it. You cannot delete an object that other metadata depends on, such as a field referenced in an Apex class, until you remove those dependencies.
- Org limits. Each Salesforce edition caps the number of custom objects. Check the limit before you plan many objects.
- Missing permissions. If a user reports "the object does not exist" or sees no tab, check the permission set before suspecting the object.
Summary of the sequence
- Create the object with correct label, API name and Name field type.
- Enable reports, search and activities if needed.
- Add fields and relationships.
- Adjust the layout.
- Grant access through a permission set.
- Verify with Apex or a query.
Exercise
You create a custom object with Object Name Shipment and a Text record name. A colleague asks whether two Shipment records can have the same Name, and what the API name of the object is. What do you answer, and what would you change if Names must be generated as SHP-0001, SHP-0002?
Show answer
Yes, two records can have the same Name, because Name is a label for people and the unique key is the record Id. The API name is Shipment__c, since Salesforce appends __c to the Object Name. To generate values like SHP-0001, use an Auto Number record name with display format SHP-{0000} and a starting number of 1. Users then cannot edit the value.
Exercise
After you deploy Invoice__c, a sales user says they cannot find it anywhere. The object is Deployed and has a tab. Name two likely causes and how you would fix them.
Show answer
The user probably has no object permission, so assign a permission set that grants Read on Invoice. Another cause is that the tab or object is not in the user's Lightning app, so add it through App Manager. A third possibility is missing field permissions, which hide fields but not the object.