Sub topic 3
Creating custom objects in Object Manager
This chapter assumes you can move around Setup, know the difference between a table, a row and a column, and have read the previous chapters on how Salesforce stores data and on standard objects. You will build an Invoice custom object, add a field, and confirm the result.
What a custom object is
A custom object is a table you define for data that the standard objects do not cover. Each record is a row. Each field is a column. Salesforce gives every custom object an API name that ends in __c, an Id field, audit fields (CreatedDate, CreatedById, LastModifiedDate, LastModifiedById) and an optional Name field.
You create and manage custom objects in Object Manager. Open Setup, then choose Object Manager in the top tabs. The list shows standard and custom objects together. Custom objects carry the Custom Object type label.
Create the object
- In Setup, open Object Manager.
- Click Create, then Custom Object.
- Enter the Label:
Invoice. - Enter the Plural Label:
Invoices. - Check the Object Name. Salesforce fills in
Invoicefrom the label. This becomes the API nameInvoice__c. - Under Record Name, set the label to
Invoice Numberand the Data Type toText. UseAuto Numberinstead if you want Salesforce to generate values such asINV-0001. - Choose the optional features you need, such as Allow Reports, Allow Activities and Track Field History.
- Set Deployment Status to
Deployedwhen you want users to see the object. - Click Save.
The object name cannot be changed casually after you save it. Code, reports and integrations refer to the API name, so pick it carefully.
Add a custom field
An object with only a Name is rarely useful. Add fields from the object's Fields & Relationships page.
- Open Object Manager, then Invoice, then Fields & Relationships.
- Click New.
- Choose a data type, for example
Currency, and click Next. - Enter the label
Amount. The API name becomesAmount__c. - Set the length and decimal places, then click Next.
- Choose which profiles can see and edit the field, then click Next.
- Add the field to page layouts, then click Save.
Repeat the steps for Status (a Picklist with the values Open, Paid and Void) and Account (a Lookup to Account). Relationships get their own chapter, so treat the lookup as a given for now.
What Salesforce builds for you
Creating the object also creates a default page layout, a list view, a tab option and a set of API endpoints. It does not create a tab or app menu entry for you in every case, and it does not grant access to anyone except users with the Modify All Data permission or the Modify All permission on the object. Users still need permissions before they can see the data.
Verify the object in code
You can confirm what you created with a describe call. This anonymous Apex snippet checks that Invoice__c exists and is custom.
Schema.DescribeSObjectResult d = Invoice__c.SObjectType.getDescribe();
System.debug(d.getName() + ' custom? ' + d.isCustom());
System.debug('Label: ' + d.getLabel());The first debug line prints Invoice__c custom? true.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The object is missing from the App Launcher | No tab exists, or the app does not include it | Create a custom tab and add it to the app |
| Users get "insufficient privileges" | No object permission granted | Assign a permission set with Read and Create on `Invoice__c` |
| Apex says `Invalid type: Invoice__c` | Wrong API name, or the code runs in an org where the object is not deployed | Check the name in Object Manager and the deployment status |
| A field is missing on the record page | The field is not on the page layout | Add it in the page layout editor |
Exercise
You created the label Invoice Number for the Record Name and later find that Apex code cannot reference Invoice_Number__c. What is the likely explanation?
Show answer
The Record Name field is the standard Name field, so code refers to it as Name. Only fields you add yourself under Fields & Relationships get a __c API name.