Relationships

We have already discussed Force.com is an object-oriented relational database management system. The backbone of the entire system based on Force.com is in relationships.

Relationships

Relationships link two different objects with each other. For example, a library user can also be a library card holder. To represent this relationship we need to assign a customer record to a user record, this is achieved using a relationship.

There are two main types of relationships:

Lookup (1:n)

The Lookup relationship can be visualized in the following image:

Lookup (1:n)

Lookup relationships are loosely coupled relationships. For example, a card of a member can have multiple penalties (fines) on it. The penalties on the card are shown at the bottom of the card in a list.

Lookup relationships have the following properties:

  • The two objects records have no relationship dependencies on each other
  • The lookup field value is optional
  • Updating and deleting the child record has no effect on the parent
  • Both objects have their own owners and sharing rules
  • A record can have 25 lookup fields with 25 different objects

Master-Detail relationship (1:n)

The following image illustrates the Master-Detail relationship:

Master-Detail relationship (1:n)

Master-Detail relationships are more tightly coupled relationships. For example, the relationship between the customer and the card in the library management will be Master-Detail. Where the customer is the master and the card is the child in the relationship.

Master-Detail relationships have the following properties.

  • When two objects are in Master-Detail relationship (also called master-child relationship) the detail/child follows the master.
  • The ownership of the child record is given to the master objects owner. The child does not have separate owner.
  • The detail inherits the sharing, security settings as well as the look and feel of the master.
  • A master record is always required for storing the child record.

    Note

    While creating a Master-Detail relationship, there must be absolutely no record of the object. If there are some records, create a lookup instead. Fill the lookup fields of all the records and then convert it into a Master-Detail.

  • If the master record is deleted, the child record is also deleted.
  • In a Master-Detail relationship between a standard object and custom object, the custom object will always be the detail record.
  • An object can maximum of two master objects. In cases of multiple objects, both the masters are required. If one master is deleted, the child object gets deleted immediately.
  • A Master-Detail can be multiple level deep, parent-child-grand-children.
    • If the master is standard object: two multiple level deep.
    • If the master is child object: three multiple level deep.

Special relationships

Using the Master-Detail and lookup relationships we can create two more types of relationships:

  • Self-relationship:
    • When an object has a lookup with itself, it is a self-relationship. A self relationship creates a tree diagram of the objects. For example, the account has a lookup on himself, called Parent Account.
    • On the user object a special self-relationship called the hierarchical relationship helps create superior roles such as supervisor, manager on user object. The hierarchical relationship is a self-relationship of the user object.
  • Many-to-many relationships:
    • A many-to-many relationship between two objects is a relationship in which an object A can have many child objects B, while object B can have multiple child objects A as shown in the figure below:
      Special relationships

A real world example is the relationship between student and teacher. A student can be taught by multiple teachers while a teacher can teach multiple students. Normally lookup and master child relationships are 1:N, however, a small trick can create a many-to-many relationship on Force.com. The trick to create the master child object is as follows:

  • First create a junction object, which contains a link to two master-objects
  • Create the name field as auto-number in the junction object

Some properties of the junction objects are as follows.

  • The junction object identifies the first master as the primary master. It uses the look and feel of the primary master.
  • If any master record is deleted, the junction record is automatically deleted.

Exercise – creating a junction object

Create a customer object to collect information about customer. Use the maximum field types described above to create fields.

Create a many-to-many relationship between customer and media. This object will be used for checkout and check in of the media item.

Exercise – building the data model for the Library Management System

Refer back to the requirements and understand what you have to build. The requirement contains different objects, lookups, master-child relationships, and junction objects. Use the information from the chapter and build the basic data model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset