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 term | Salesforce term | Example |
|---|---|---|
| Table | Object | Account, Menu_Item__c |
| Column | Field | Price__c |
| Row | Record | The Espresso menu item |
| Primary key | Record Id | 18-character id such as a0B... |
| Foreign key | Relationship (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
| Type | Stores | Note |
|---|---|---|
| Text, Text Area, Long Text Area | Words | Pick the right length |
| Number, Currency, Percent | Numbers | You choose decimal places |
| Checkbox | True or false | Defaults to unchecked |
| Date, Date/Time | Dates and times | Store times in one consistent way |
| Picklist | One value from a list | Keeps data clean; multi-select picklists also exist |
| Email, Phone, URL | Validated formats | Clickable on screen |
| Formula | A value calculated from other fields | Read-only, always up to date |
| Roll-up Summary | COUNT, SUM, MIN or MAX of child records | Only on the master of a master-detail relationship |
| Lookup, Master-Detail | A link to another record | Covered below |
Relationships
Lookup versus master-detail
| Feature | Lookup | Master-detail |
|---|---|---|
| Strength | Loose link | Tight parent and child bond |
| Parent required? | Optional (unless you make it required) | Always required |
| Delete the parent | Child stays (the link is cleared, by default) | Children are deleted too |
| Owner and sharing of child | Own owner | Inherits from the parent; no owner field |
| Roll-up summary on the parent | Not available as a field type | Available |
| Typical use | Order points to a Contact | Order 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) | Field | Type |
|---|---|---|
| Menu Item (Menu_Item__c) | Name | Text (record name) |
| Menu Item (Menu_Item__c) | Category__c | Picklist: Coffee, Tea, Bakery, Food |
| Menu Item (Menu_Item__c) | Price__c | Currency |
| Menu Item (Menu_Item__c) | Available__c | Checkbox (default checked) |
| Coffee Order (Coffee_Order__c) | Name | Auto Number, format ORD-{0000} |
| Coffee Order (Coffee_Order__c) | Customer__c | Lookup to Contact |
| Coffee Order (Coffee_Order__c) | Status__c | Picklist: Received, Preparing, Ready, Picked up, Cancelled |
| Coffee Order (Coffee_Order__c) | Pickup_Time__c | Date/Time |
| Coffee Order (Coffee_Order__c) | Total__c | Roll-up Summary: SUM of Order Item Line_Total__c |
| Order Item (Order_Item__c) | Name | Auto Number, format OI-{0000} |
| Order Item (Order_Item__c) | Coffee_Order__c | Master-Detail to Coffee Order |
| Order Item (Order_Item__c) | Menu_Item__c | Lookup to Menu Item |
| Order Item (Order_Item__c) | Quantity__c | Number (3, 0) |
| Order Item (Order_Item__c) | Unit_Price__c | Currency |
| Order Item (Order_Item__c) | Line_Total__c | Formula (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
- 1Click the gear icon, choose Setup, open Object Manager.
- 2Click Create, then Custom Object.
- 3Label:
Menu Item. Plural Label:Menu Items. The Object Name fills in asMenu_Item. - 4Record Name: label
Menu Item Name, data type Text. - 5Under Optional Features, tick Allow Reports and Allow Search. Leave Deployment Status as Deployed.
- 6Under Object Creation Options, tick Launch New Custom Tab Wizard after saving this custom object, and click Save.
- 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
- 1In Object Manager, open Menu Item, then Fields & Relationships, then New.
- 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. - 3Create a Currency field labelled
Price, length 6 and decimal places 2. - 4Create a Checkbox field labelled
Availablewith default value Checked.
Step 3: create Coffee Order and Order Item
- 1Create a custom object
Coffee Order(pluralCoffee Orders). For Record Name choose data type Auto Number, display formatORD-{0000}, starting number 1. Tick Allow Reports, Allow Activities and Allow Search, and save with a tab. - 2Add to Coffee Order a Lookup field to Contact, labelled
Customer. Add a PicklistStatuswith the five values in the table. Add a Date/Time fieldPickup Time. - 3Create a second custom object
Order Item(pluralOrder Items) with Record Name type Auto Number and formatOI-{0000}. Tick Allow Reports. - 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 nameCoffee_Order__c. - 5Create a Lookup Relationship field from Order Item to Menu Item. Save.
- 6Add Number field
Quantity(length 3, 0 decimal places) and Currency fieldUnit Price(6, 2). - 7Add a Formula field
Line Total. The return type is Currency with 2 decimal places, and the formula isQuantity__c * Unit_Price__c. Use Check Syntax, then save.
Quantity__c * Unit_Price__cA 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
- 1Open the Coffee Order object, then Fields & Relationships, New.
- 2Choose Roll-Up Summary and click Next. Label it
Total. - 3Choose the summarised object Order Items, roll-up type SUM, field to aggregate Line Total.
- 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
- 1In Setup, type
App Managerin Quick Find, click New Lightning App. - 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. - 3Open the app from the App Launcher. Under Menu Items, create
Espresso(Coffee, 120),Cappuccino(Coffee, 180) andButter Croissant(Bakery, 150). - 4Create a contact (or use Asha Rao). Create a Coffee Order with that customer, status Received.
- 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.
- 6Refresh the order. Total should read 390.
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
- Check for a standard object first before creating a custom one.
- Use picklists instead of free text where values are a fixed list.
- Choose master-detail only when the child cannot exist without its parent.
- Prefer formula fields to storing values you can calculate.
- Write clear labels, and add field descriptions and help text for admins and users.
- 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.
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
- Objects are tables, fields are columns and records are rows; custom objects and fields end in __c.
- Lookup is a loose link; master-detail ties child to parent (child is deleted with the parent, inherits sharing and allows roll-ups).
- A junction object with two relationships models many-to-many.
- Formula fields calculate values; roll-up summaries aggregate child records on a master-detail parent.
- 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.