SFDC: Relate Objects
In my previous post SFDC: Creating a New Object , we created objects that stood on their
own. The fields on the Merchandise
object had no relation to the fields on the Invoice object. In this post, we create a Line Item object, and what’s special about this new object is
that its fields are related 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
already saw something similar in the Status field. When you create a new
invoice, you can choose a status from the picklist. A lookup field is different
because the values come dynamically from a custom object rather than statically
from a picklist.
Create the Line Item Object
Each invoice is made up of a number of invoice line items,
which represent the number of merchandise items being sold at a particular price.
You are first going to create the Line
Item object, and then relate it to the Invoice and Merchandise
From Setup, click Create > Objects.
Custom Object and fill in the custom object definition.
Label: Line Items
Name: Line Item Number
In the Optional
Features section, select Allow
Reports, and click Save.
might be wondering why Line Item Number is a text field, when what you enter is
a number. If line items are numbered, why not make it an auto-number field,
like Invoice? The short answer is that it’s easier to work with text when
working with records.
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. Recall that the
Merchandise object also has a Quantity field to track how many items are in
stock, and the steps to create the field are the same.
On the Line Item detail page, scroll down to Custom Fields and Relationships and
Type, select Number and click Next.
Fill in the field details:
Leave the defaults for the remaining fields, and
click Next, Next, and then Save.
Relate Line Items to Invoice
Now that you have all the objects representing the data
model, you need to relate them to each other. Line items are related to both an
invoice (an invoice is composed of a number of line items) and merchandise (a
line item takes its price from the merchandise).
On the detail page of the Line Item object,
scroll down to the Custom Fields &
Relationships related list and click New.
Type, select Master-Detail
Relationship and click Next.
In the Related
To field, select your Invoice custom
object, and click Next.
Label and Field Name enter Invoice.
Accept the defaults on the next three screens by
On the final screen click Save & New.
One way to think of this master-detail relationship is that an invoice now “owns” its line
items. In other words, an invoice can now contain multiple line items. One of
the neat things about master-detail relationships is that they support roll-up
summary fields, allowing you to aggregate information about the child records.
Look Up Merchandise Items
The other kind of relationship you need to create 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 & New, so you should already be on the New
Custom Field dialog.
Type, select Lookup Relationship
and click Next.
In the Related
To field, select Merchandise and
Label and Field Name enter Merchandise.
Verify your screen looks like the following
image and then click Next.
Accept the defaults on the subsequent screens by
clicking Next, and Next again.
On the final screen, deselect the checkboxes for
Merchandise Layout and Append related list to users’ existing
personal customizations (you don’t want a list of line items on the
this point you have two relationships, a master-detail relationship that allows
an invoice record to contain many line items, and a lookup relationship that
relates a particular line item to a piece of merchandise.
As you saw in the previous post, the platform automatically
generates a user interface for the objects you create, so that you can view, edit,
delete, and update records. Because you also related the objects, the user
interface provides a way of navigating between related records as well. You can
see how all that works by creating another invoice record.
Click the Invoices
tab and then New and Save.
Item Number, type 1.
In the Merchandise
field, type the first few letters of laptop
and click the Find icon.
and then Save.
you’re familiar with the products in your inventory, you can type the first few
letters of a piece of merchandise and click Save. You don’t need to click the Find icon, the system automatically finds the merchandise and adds
it when you save the record. There’s a lot of built-in functionality!
View the Schema
You now have three custom 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.”
From Setup, click Schema Builder.
In the left pane, click Clear All to remove the standard objects from the schema.
Select the checkboxes for Merchandise, Invoice, and Line Item.
to arrange the objects, or manually adjust the layout if necessary
**This document is referred from salesforce help tutorials