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 objects.
1. From Setup, click Create > Objects.
2. Click New Custom Object and fill in the custom object definition.
a. Label: Line Item
b. Plural Label: Line Items
c. Record Name: Line Item Number
d. Data Type: Text
3. In the Optional Features section, select Allow Reports, and click Save.
Note: You 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.
1. On the Line Item detail page, scroll down to Custom Fields and Relationships and click New.
2. For Data Type, select Number and click Next.
· Fill in the field details:
· Field Label: Quantity
4. Select Required
3. 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).
1. On the detail page of the Line Item object, scroll down to the Custom Fields & Relationships related list and click New.
2. For Data Type, select Master-Detail Relationship and click Next.
3. In the Related To field, select your Invoice custom object, and click Next.
4. For Field Label and Field Name enter Invoice.
5. Accept the defaults on the next three screens by clicking Next.
6. 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.
1. For Data Type, select Lookup Relationship and click Next.
2. In the Related To field, select Merchandise and click Next.
3. For Field Label and Field Name enter Merchandise.
4. Verify your screen looks like the following image and then click Next.
5. Accept the defaults on the subsequent screens by clicking Next, and Next again.
6. 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 Merchandise page).
7. Click Save
Note: At 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.
1. Click the Invoices tab and then New and Save.
2. Click New Line Item.
3. For Line Item Number, type 1.
4. For Quantity, type 2.
5. In the Merchandise field, type the first few letters of laptop and click the Find icon.
6. Click Laptop and then Save.
Note: If 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.”
1. From Setup, 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 arrange the objects, or manually adjust the layout if necessary
**This document is referred from salesforce help tutorials