Salesforce Development
Hands-onBeginner 40 min· Platform basics · 3 of 17

Data model: objects, fields and relationships

Learn how Salesforce stores data in objects, fields and relationships, then build the Brew & Bean ordering data model in your own org: custom objects, lookup and master-detail relationships, a formula field and a roll-up summary.

In this tutorial you will build a small ordering data model for the Brew & Bean coffee shop with clicks only: a Menu Item object, a Coffee Order object, and an Order Item object that links them. You will use lookup and master-detail relationships, a formula field and a roll-up summary.

Prerequisite: a Developer Edition org (see *Get a free Developer Edition org and start Trailhead*). Time to build: about 40 minutes.

The building blocks

If you know databases, the mapping is simple. An object is like a table, a field is like a column and a record is like a row. Salesforce creates the real database tables for you from this metadata.

Objects come in two kinds. Standard objects are built in: Account (a company), Contact (a person), Lead (an unqualified prospect), Opportunity (a potential sale), Case (a support issue), and many more. Custom objects are the ones you create. Their API names end in __c, as do custom fields (for example Menu_Item__c and Price__c).

Database words and Salesforce words

Database termSalesforce termExample
TableObjectAccount, Menu_Item__c
ColumnFieldPrice__c
RowRecordThe Espresso menu item
Primary keyRecord Id18-character id such as a0B...
Foreign keyRelationship (lookup or master-detail)Order Item points to its Coffee Order

Every object has an Id field and a name field, plus system fields such as CreatedDate and OwnerId, filled in automatically.

Field types you will use most

Common field types

TypeStoresNote
Text, Text Area, Long Text AreaWordsPick the right length
Number, Currency, PercentNumbersYou choose decimal places
CheckboxTrue or falseDefaults to unchecked
Date, Date/TimeDates and timesStore times in one consistent way
PicklistOne value from a listKeeps data clean; multi-select picklists also exist
Email, Phone, URLValidated formatsClickable on screen
FormulaA value calculated from other fieldsRead-only, always up to date
Roll-up SummaryCOUNT, SUM, MIN or MAX of child recordsOnly on the master of a master-detail relationship
Lookup, Master-DetailA link to another recordCovered below

Relationships

Lookup versus master-detail

FeatureLookupMaster-detail
StrengthLoose linkTight parent and child bond
Parent required?Optional (unless you make it required)Always required
Delete the parentChild stays (the link is cleared, by default)Children are deleted too
Owner and sharing of childOwn ownerInherits from the parent; no owner field
Roll-up summary on the parentNot available as a field typeAvailable
Typical useOrder points to a ContactOrder Item belongs to one Order

A many-to-many relationship is built with a junction object: an object that has two relationships, one to each side. Our Order Item is a junction between Coffee Order and Menu Item: an order has many items, and a menu item appears in many orders. Classic junctions use two master-detail fields. In our build we use master-detail to the order and a lookup to the menu item, so that deleting a menu item does not silently delete past order lines.

Other relationship types exist (hierarchical on the User object, and external lookups for data outside Salesforce) but you will use lookup and master-detail most.

Build the model

This is the model you will create. Brew & Bean prices and categories match the shared example used across this site.

Objects and fields

Object (API name)FieldType
Menu Item (Menu_Item__c)NameText (record name)
Menu Item (Menu_Item__c)Category__cPicklist: Coffee, Tea, Bakery, Food
Menu Item (Menu_Item__c)Price__cCurrency
Menu Item (Menu_Item__c)Available__cCheckbox (default checked)
Coffee Order (Coffee_Order__c)NameAuto Number, format ORD-{0000}
Coffee Order (Coffee_Order__c)Customer__cLookup to Contact
Coffee Order (Coffee_Order__c)Status__cPicklist: Received, Preparing, Ready, Picked up, Cancelled
Coffee Order (Coffee_Order__c)Pickup_Time__cDate/Time
Coffee Order (Coffee_Order__c)Total__cRoll-up Summary: SUM of Order Item Line_Total__c
Order Item (Order_Item__c)NameAuto Number, format OI-{0000}
Order Item (Order_Item__c)Coffee_Order__cMaster-Detail to Coffee Order
Order Item (Order_Item__c)Menu_Item__cLookup to Menu Item
Order Item (Order_Item__c)Quantity__cNumber (3, 0)
Order Item (Order_Item__c)Unit_Price__cCurrency
Order Item (Order_Item__c)Line_Total__cFormula (Currency): Quantity__c * Unit_Price__c

Why not an object called Order?

Salesforce already has a standard Order object. A custom Order__c would be allowed, but two things called Order in reports, code and interviews cause confusion. Choose a distinct name like Coffee Order.

Step 1: create the Menu Item object

  1. 1Click the gear icon, choose Setup, open Object Manager.
  2. 2Click Create, then Custom Object.
  3. 3Label: Menu Item. Plural Label: Menu Items. The Object Name fills in as Menu_Item.
  4. 4Record Name: label Menu Item Name, data type Text.
  5. 5Under Optional Features, tick Allow Reports and Allow Search. Leave Deployment Status as Deployed.
  6. 6Under Object Creation Options, tick Launch New Custom Tab Wizard after saving this custom object, and click Save.
  7. 7In the tab wizard, choose any tab style and finish. A tab lets users open the object from the navigation bar.

Step 2: add fields to Menu Item

  1. 1In Object Manager, open Menu Item, then Fields & Relationships, then New.
  2. 2Choose Picklist, label Category, enter the values Coffee, Tea, Bakery, Food (one per line). Next, accept the field-level security defaults, and add the field to the default page layout. Save.
  3. 3Create a Currency field labelled Price, length 6 and decimal places 2.
  4. 4Create a Checkbox field labelled Available with default value Checked.

Step 3: create Coffee Order and Order Item

  1. 1Create a custom object Coffee Order (plural Coffee Orders). For Record Name choose data type Auto Number, display format ORD-{0000}, starting number 1. Tick Allow Reports, Allow Activities and Allow Search, and save with a tab.
  2. 2Add to Coffee Order a Lookup field to Contact, labelled Customer. Add a Picklist Status with the five values in the table. Add a Date/Time field Pickup Time.
  3. 3Create a second custom object Order Item (plural Order Items) with Record Name type Auto Number and format OI-{0000}. Tick Allow Reports.
  4. 4On Order Item, create a field of type Master-Detail Relationship. Choose Coffee Order as the related object. Accept the defaults. The field is labelled Coffee Order. Name it with the API name Coffee_Order__c.
  5. 5Create a Lookup Relationship field from Order Item to Menu Item. Save.
  6. 6Add Number field Quantity (length 3, 0 decimal places) and Currency field Unit Price (6, 2).
  7. 7Add a Formula field Line Total. The return type is Currency with 2 decimal places, and the formula is Quantity__c * Unit_Price__c. Use Check Syntax, then save.
Formula for Line_Total__c
Quantity__c * Unit_Price__c

A formula field recalculates whenever its inputs change. If Quantity__c can be blank, wrap it: BLANKVALUE(Quantity__c, 0) * Unit_Price__c.

Step 4: add a roll-up summary on Coffee Order

  1. 1Open the Coffee Order object, then Fields & Relationships, New.
  2. 2Choose Roll-Up Summary and click Next. Label it Total.
  3. 3Choose the summarised object Order Items, roll-up type SUM, field to aggregate Line Total.
  4. 4Under filter criteria choose All records should be included in the calculation, and save. Make the field visible when asked.

Make it an app and test it

Step 5: build an app and add data

  1. 1In Setup, type App Manager in Quick Find, click New Lightning App.
  2. 2Name it Brew and Bean, accept defaults through the wizard, and in Navigation Items add Menu Items, Coffee Orders, Order Items and Contacts. Add your user profile on the User Profiles step and save.
  3. 3Open the app from the App Launcher. Under Menu Items, create Espresso (Coffee, 120), Cappuccino (Coffee, 180) and Butter Croissant (Bakery, 150).
  4. 4Create a contact (or use Asha Rao). Create a Coffee Order with that customer, status Received.
  5. 5On the order's page, in the Order Items related list, click New. Pick Espresso, quantity 2, unit price 120. Then add Butter Croissant, quantity 1, unit price 150.
  6. 6Refresh the order. Total should read 390.
What you should see
Each Order Item shows a Line Total (240 for the espresso line, 150 for the croissant). The Coffee Order record shows Total as 390.00 (in your org's currency). Deleting the Coffee Order would also delete both Order Items, because of the master-detail link.

Why copy the price onto the order item?

Menu prices change. By storing Unit Price on the order line, past orders keep the price the customer actually paid. Later you can use a Flow to fill it in automatically from the Menu Item.

Design habits

Data model checklist

  1. Check for a standard object first before creating a custom one.
  2. Use picklists instead of free text where values are a fixed list.
  3. Choose master-detail only when the child cannot exist without its parent.
  4. Prefer formula fields to storing values you can calculate.
  5. Write clear labels, and add field descriptions and help text for admins and users.
  6. Remember the API name, because Flow, SOQL and Apex all use it.

How this comes up in interviews. Expect "Lookup versus master-detail?", "What is a junction object?", "What is a roll-up summary and where can it be used?" and "How do you model many-to-many?". Answer from the table above and mention your coffee-shop example.

Prompt · Review my data model
I'm designing a Salesforce data model for [business, e.g. a college event registration app].

Objects and fields so far:
[paste your list]

Review it as a Salesforce architect. Tell me: which relationships should be lookup versus master-detail and why, whether a standard object (Account, Contact, Opportunity, Case) could replace any of my custom objects, and any fields that should be formulas, roll-ups or picklists. Give the answer as a short table of changes.

What to expect: Check any claim about limits or standard-object behaviour on Salesforce Help before relying on it.

Try it: Extend the model

1. Add a validation rule on Order Item that blocks saving when Quantity is less than 1. (Setup > Object Manager > Order Item > Validation Rules.) 2. Add a roll-up summary on Coffee Order called Item Count that sums Quantity. 3. Add a field Cancellation Reason (text) to Coffee Order. 4. Build a many-to-many example of your own: students and courses with a junction object Enrollment.

Key takeaways

  1. Objects are tables, fields are columns and records are rows; custom objects and fields end in __c.
  2. Lookup is a loose link; master-detail ties child to parent (child is deleted with the parent, inherits sharing and allows roll-ups).
  3. A junction object with two relationships models many-to-many.
  4. Formula fields calculate values; roll-up summaries aggregate child records on a master-detail parent.
  5. Use standard objects when they fit and plan the model before clicking.

Check yourself

Finishing the quiz marks the lesson as done.

1. Which field type can aggregate (SUM, COUNT, MIN, MAX) child records on the parent?

2. What happens to child records when you delete the parent in a master-detail relationship?

3. How do you model a many-to-many relationship between Students and Courses?

4. What does the __c suffix on Menu_Item__c mean?

5. Why use a lookup rather than master-detail from Order Item to Menu Item in our model?

6. You need a field that always shows Quantity times Unit Price. Which is best?

Saved on this device. Create a free account to keep it on every device.

Practise this with Sam

A real mock interview, out loud, with an honest scorecard. Free to start.

Start free