In previous tutorials you created objects that stood more or less on their own. In this tutorial, you create a Line Item object and then relate it to both the Invoice and Merchandise objects.

  • An invoice has one or more line items. In fact, you might say that a particular invoice "owns" its line items. That kind of relationship is called a master-detail relationship, where the detail records refer to a master record.
  • Line items also relate to merchandise through another kind of relationship called a "lookup." You've already seen something similar in the Status field; when you create a new invoice, you can choose a status from the list. A lookup field is different because the values come dynamically from a custom object rather than statically from a picklist. If this is confusing now, it will all be very clear in a moment.


You must complete all previous tutorials in this series, starting with Quick Tutorial 1: Build a Cloud App in 5 Minutes.

Step 1: Create the Line Item Object

  1. Click <your_name> | Setup | Create | Objects and then click New Custom Object.
  2. Fill in the details as shown in the following image:

QS4 line item.png

The last time you created a custom object, you created an associated custom tab right away. Because line items should only appear within the context of an invoice, you don't need (or want) to view line items by themselves, so ignore the option for creating a tab.

Step 2: Add a Quantity Field

Every line item needs to track the quantity ordered. So the next thing you need to do is add a Quantity field. You already know how to do this — simply repeat Step 3 from Quick Tutorial 2: Build A Cloud Database — Add Fields to an Object.

Step 3: Relate Line Items to an Invoice

An invoice can have multiple line items. To create that relationship, specify a new field as a master-detail relationship.

  1. On the detail page for the Line Item custom object, scroll down to Custom Fields and Relationships and click New.
  2. Select Master Detail and click Next.
  3. In the Related To list, select Invoice and click Next.
  4. Accept the defaults on the subsequent screens by clicking Next, Next, and then Save and New.

Step 4: Look Up Merchandise Items

The other relationship you need to implement is called a lookup. As the name implies, the field gets its information by looking it up dynamically in another object. In the last step, you used Save and New, so you should already be on the New Custom Field dialog.

  1. For the field type, select Lookup Relationship and click Next.
  2. In the Related To list, select Merchandise. Your screen should look like the following:

Qs4 lookup.png

Accept the defaults on this screen by clicking Next, and on the subsequent screens by clicking Next, Next.

On the final screen, uncheck both check boxes for appending this to related lists, because you don't need a list of line items on the Merchandise page. Now you can Save.

Qs4 norelatedlist.png

Step 5: Try Out the App

Now create another invoice to see how these relationships work. Click the Invoices tab and then New and Save.

Notice there's a button to create New Line Item. Click it.

Qs3 newinvoice.png

For the Line Item Number, enter the number 1, and then click the lookup icon to choose a piece of merchandise. Note that you can also enter the first few letters of a piece of merchandise and when you save the record, the system figures it out for you. There's a lot of built-in functionality!

Qs3 lineitem.png

Tell Me More

You now have three objects, several fields, and two kinds of relationships. If you have all of that in your head, awesome. However, most people find this is an ideal time to use the old adage "a picture is worth a thousand words."

  1. Click <your_name> | Setup and in the App Setup section, click Schema Builder.
  2. In the left pane, click Clear All to remove the standard objects from the schema.
  3. Select the checkboxes for Merchandise, Invoice, and Line Item.
  4. Click Auto-Layout to separate the objects on the diagram.

Qs4 schema.png

The Schema Builder represents your objects, fields, and relationships in a standard entity-relationship diagram. In a relationship, the "crows feet" at the end of the line tells you which side is the "many" side of a one-to-many relationship (one invoice can contain multiple line items).

Note: The Schema Builder isn't just for viewing your schema, it also supports drag-and-drop development for creating new objects and fields. However, unlike the wizards you've used so far, everything you create on the Schema Builder will be invisible to users. You must set the field visibility and configure page layouts before your new objects and fields can be accessible. Field visibility and page layouts will both be covered in a future Quick Tutorial.

Next Steps

Typically, every business app enforces "rules" that prevent bad data from getting into the system. Without such rules, things can get real messy, real fast, because users might not adhere to these rules on their own. In the next tutorial, you will learn how to enforce a business rule in the Warehouse app's database, without typing in a single line of code.