Sub topic 1
How Salesforce stores data: objects, fields and records
This chapter gives you the working model of the Salesforce data model. Later chapters build on it: standard objects, custom objects, relationships, security and querying.
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.
- Optionally, some SQL-style querying experience. It helps in the later chapters.
The core mapping
Salesforce stores business data in a relational database that the platform manages for you. You never create tables directly. You work with three concepts instead, and each maps to a familiar relational idea.
| Salesforce term | Relational equivalent | Example |
|---|---|---|
| Object | Table | Account |
| Field | Column | Industry |
| Record | Row | The account named Acme Corp |
An object defines the shape of the data. A field defines one attribute of that shape and has a data type, such as text, number, date or picklist. A record is one instance of the object, with a value for each field.
Every record gets a unique Id, a 15-character case-sensitive value that Salesforce also exposes as an 18-character case-insensitive value. The Id is the primary key. Relationship fields store the Id of another record, which is how foreign keys work in Salesforce.
Standard and custom objects
Objects come in two groups.
- Standard objects ship with the platform. Account, Contact, Lead, Opportunity and Case are examples. Salesforce defines them, and they carry built-in behavior.
- Custom objects are objects you define for data the standard set does not cover. Their API names end in
__c, for exampleInvoice__c.
Both kinds work the same way once they exist. They hold fields and records, they can be related to each other, and you secure and query them with the same tools. The following chapters cover how to create and choose between them.
Seeing the model in code
You can check whether an object is standard or custom from Apex. The 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());This code assumes your org has a custom object named Invoice__c. The first debug line reports false and the second reports true.
You can also read records with SOQL, the Salesforce query language. This query returns Account records and, through a child relationship, their Contact records:
SELECT Id, Name, Industry,
(SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10Each row in the result is a record. Each name in the SELECT list is a field. Account is the object. The mapping from the table above shows up directly in the query.
Common mistakes
- Treating objects as freely joinable tables. Salesforce only joins through defined relationship fields. You cannot join two objects on arbitrary columns in SOQL.
- Confusing the label with the name used in code. The label is what users see in the interface. Code and queries use the API name. Later chapters cover this in detail.
- Comparing 15-character Ids without care. The short form is case-sensitive. The 18-character form is safe for case-insensitive comparisons, such as in spreadsheets.
Exercise
A teammate says "I need a new column on the Contact table to store a preferred language." Translate this request into Salesforce terms, and say what each part becomes.
Show answer
The table is the Contact object, so "Contact table" becomes the Contact standard object. The new column is a custom field on that object, which gets an API name ending in __c. Each existing Contact record (row) will then have a value, possibly empty, for that new field.
Exercise
What does a relationship field store, and which relational concept does it correspond to?
Show answer
A relationship field stores the Id of a record in another object. It corresponds to a foreign key.