Sub topic 1
How Salesforce stores data: objects, fields and records
This chapter gives you the working model of the Salesforce data model. It covers the three core building blocks (objects, fields and records), how they map to relational concepts, and the errors people most often make when they first work with them. Later chapters cover standard objects, custom objects, relationships and querying in detail.
Prerequisites
This chapter assumes you have:
- Basic familiarity with the Salesforce user interface and Setup.
- A general understanding of relational data: tables, rows, columns and foreign keys.
- Access to a Salesforce org where you can open Setup. A free Developer Edition org works well.
You do not need to write code to follow the chapter. A few short SOQL and Apex snippets appear near the end so you can see the same ideas from the developer side.
The core model
Salesforce stores business data in objects. An object is a definition of a kind of thing you track, such as an account, a case or an invoice. Each object has fields, which describe the attributes of that thing. Each row of actual data is a record.
The mapping to a relational database is close enough to be useful:
| Salesforce term | Relational term | Example |
|---|---|---|
| Object | Table | Account |
| Field | Column | Industry |
| Record | Row | The Account named "Acme Corp" |
| Lookup or master-detail field | Foreign key | Contact.AccountId |
| Record Id | Primary key | 001... (an 18-character Id) |
The mapping is a starting point, not an exact match. Salesforce is a multitenant platform, so you never see the physical tables. You work with a logical layer. The platform generates the Id values, enforces many of the rules, and exposes the data through the same object and field names in the user interface, in reports, in SOQL and in Apex. That consistency is the main benefit of the model: define an object once and the platform gives you screens, an API, security controls and reporting for it.
Objects
An object describes the structure and behavior shared by all of its records. When you define an object, you decide:
- Which fields it has.
- How its records are labeled and identified.
- Which relationships it has with other objects.
- Who can see and change its records.
- What page layouts, record types and automation apply to it.
Objects come in two main kinds.
Standard objects
Standard objects ship with the platform. Account, Contact, Lead, Opportunity, Case, User and Task are examples. They come with built-in behavior. For instance, converting a Lead creates an Account, a Contact and optionally an Opportunity, and that behavior exists only because Lead is a standard object. You cannot delete a standard object, but you can add custom fields to many of them.
Custom objects
Custom objects are the ones you create to hold data that the standard objects do not cover. An invoice, a training course or a piece of equipment could each be a custom object. The platform treats them as first-class: they get an API, a user interface, reports, security controls and automation, just like standard objects. The next chapters cover how to create them and how to decide between the two kinds.
The following Apex snippet shows how code can tell the two apart. The isCustom() method returns false for Account and true for Invoice__c.
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());The __c at the end of Invoice__c is a naming suffix that marks a custom object. Chapter 4 explains API names in full. For now, treat the suffix as a signal that someone in your org created the object.
Fields
A field holds one attribute of a record. Every field has a label, which is the text users see, and an API name, which is what code and integrations use. Every field also has a data type that controls what values it accepts.
Common data types include:
- Text and Text Area for strings of different lengths.
- Number, Currency and Percent for numeric values.
- Date and Date/Time for temporal values.
- Checkbox for true and false.
- Picklist and Multi-Select Picklist for values chosen from a list.
- Lookup and Master-Detail for links to other records.
- Formula for read-only values calculated from other fields.
Fields are also standard or custom. Standard fields exist on the object from the start (for example Name, Industry and CreatedDate on Account). Custom fields are ones you add, and their API names end in __c.
System fields
Every record carries system fields that you do not create and cannot freely edit. The most important are:
Id: the unique identifier of the record.CreatedDateandCreatedById: when and by whom the record was created.LastModifiedDateandLastModifiedById: when and by whom it was last changed.OwnerId: the user or queue that owns the record, on objects that support ownership.IsDeletedandSystemModstamp: used for soft deletion and change tracking.
You can read these fields in queries and use them in reports and filters. Treat them as part of the data model even though you never add them yourself.
The Name field
Each object has a Name field that acts as the record's label in lists and lookups. On Account it is a text field. On a custom object you choose between a text name and an auto-number name such as INV-{0000}. This is the one field you must decide on when you create a custom object, and it is hard to change the data type later without side effects. The Name field is not a unique key, so two records can have the same name.
Records
A record is one instance of an object: one account, one invoice, one case. Each record has a unique Id, which is the primary key. Ids come in two forms, a 15-character case-sensitive form and an 18-character case-insensitive form. The API returns the 18-character form, and you should prefer it when you compare or store Ids outside Salesforce, for example in a spreadsheet that ignores case.
The first three characters of an Id are a key prefix that identifies the object. Account records start with 001, Contact records with 003 and Opportunity records with 006. Custom objects get a prefix that is unique to your org, so the same custom object can have a different prefix in a sandbox and in production.
Records are created, read, updated, deleted and undeleted through the user interface, the API or Apex. When you delete a record, Salesforce moves it to the Recycle Bin for a limited time before it is purged, which is why IsDeleted exists.
Here is how a record is created in Apex. The code inserts a standard Account and then a custom Invoice__c record linked to it. It assumes Invoice__c has a text Name field, a lookup field Account__c, a number field Amount__c and a picklist field Status__c.
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;After insert acc, the platform fills in acc.Id. That value is what makes the link on the invoice possible. This is the same pattern as inserting a parent row and then using its generated key in a child row.
Seeing the model from a query
SOQL, the Salesforce Object Query Language, reads records using object and field names. It looks like SQL but has no joins in the usual sense. You follow relationships through names instead. This query reads Account and uses the child relationship name Contacts to return each account's contacts:
SELECT Id, Name, Industry,
(SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10The same idea works on a custom object. Here, Account__r.Name reaches the Name field of the parent Account through the Account__c lookup:
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'Notice that Account__c holds the Id, while Account__r lets you traverse to the related record's fields. Chapters 6 and 10 return to this distinction.
Where the data model lives in Setup
You manage objects and fields in Object Manager, which you open from Setup. Selecting an object shows its fields and relationships, page layouts, record types and more. This is the place to confirm an object's API name, check a field's data type, and see which fields are standard and which are custom.
A useful habit is to open the Fields & Relationships page for an object and compare the Field Label, Field Name (the API name) and Data Type columns. The label can change at any time without breaking anything. The API name is what your code depends on.
Common errors and gotchas
- Confusing label and API name. Renaming an object's label in the user interface does not change its API name. Code that refers to
Invoice__ckeeps working even if users now see "Bill". The reverse also bites: a label like "Invoice" tells you nothing about whether the API name isInvoice__corInvoice_Header__c. - Assuming a record Id is a stable cross-org key. Ids differ between orgs and sandboxes. Never hard-code them in code or data files that move between environments. Use an external ID field or a lookup by a business key instead.
- Treating Name as unique. It is a label, not a key. Duplicate names are allowed unless you add duplicate rules or a unique custom field.
- Comparing 15-character Ids as case-insensitive. The 15-character form is case sensitive. Two different records can differ only by letter case.
- Expecting SQL joins. SOQL follows defined relationships. You cannot join two objects that have no relationship between them.
- Forgetting that access is not automatic. A new custom object is invisible to most users until you grant permissions. If a query returns nothing, or a field throws an error, check object and field security before assuming the data is missing.
How this chapter connects to the rest
The model you have just seen is the base for everything that follows. Standard and custom objects share the same field and record behavior. They differ in who defines them and in what extra behavior comes built in. The next chapters cover the standard objects you will meet most often, how to create custom objects in Object Manager, the naming rules for API names, and how relationships connect objects into a working schema.
Exercises
Exercise
A colleague says "an object is just a table, and a record is just a row, so I can ignore everything else." Name two things the Salesforce data model provides for an object that a plain database table does not give you by itself.
Show answer
Any two of the following: an automatically generated user interface (page layouts and record pages), an API exposed through the same names, built-in security controls at the object and field level, reporting, and automation support. The platform adds these when you define the object.
Exercise
You see the field Account__r.Name in a SOQL query on Invoice__c. What does Account__c store, and what does Account__r let you do?
Show answer
Account__c stores the Id of the related Account record, like a foreign key. Account__r is the relationship name that lets the query traverse to the parent Account record and read its fields, such as Name.
Exercise
You deploy code from a sandbox to production. The code contains the hard-coded Id of a record, and it fails in production. Why, and what should you use instead?
Show answer
Record Ids are generated per org, so the same record has a different Id in production than in the sandbox. Use an external ID field or another business key to find the record, and look up its Id at run time.