Create Object Relationships

-Based on Salesforce Trailheads-

The force.com platform differs from traditional databases in that it uses relationship fields rather than primary and foreign keys to establish relationships. A relationship field is a custom field on an object which contains a link to another record.

Relationships allow you to display data about other related object records on a particular record’s detail page. It works by storing the ID of the parent record and optionally providing user interface representations in both the parent and child records.

There are two main types of relationship fields:

  • Lookup. Links one object to another object in a looser fashion. Allows related lists etc.
  • Master-Detail. Links one object to another more closely. This means that child inherits various settings from the parent, is deleted if its deleted etc. Master-Detail is the only one that can use rollup summary fields.

Master-Detail is usually used when there is a direct dependency between two objects. Lookup relationships are appropriate when a relationship between two objects is required in some cases, but not always.

To create a Lookup relationship:

  1. Go to Setup > Objects
  2. Click the object that will be child object
  3. In the Custom Fields & Relationships related list, click New
  4. Select Lookup Relationship, and click Next
  5. In the Related To drop-down list, choose User, then click Next
  6. Enter Field Label
  7. Click Next
  8. Click Save

To create a Master-Detail relationship:

  1. Go to Setup > Objects
  2. Click Child Object
  3. In the Custom Fields & Relationships related list, click New
  4. Select Master-Detail Relationship, and click Next
  5. In the Related To drop-down list, choose parent object, and click Next
  6. Click in the Field Name text box and enter the field name
  7. Select the Read/Write radio button
  8. Check Child records can be reparented to other parent records after they are created if you want to be able to change the relationship field’s value. If you leave this box unchecked, you can’t change the value in the future.
  9. Click Next
  10. Accept the defaults in the remaining three steps of the wizard.
  11. Click Save

There is also something called many-to-many relationships. This is when a record can be associated with several of another object’s records. And that object’s records can in turn be associated with many of the first object’s records. For example: one position could be posted to many employment websites and one employment website could list many positions.

This is generally achieved by creating a junction object then creating two master detail relationships for it.

To create a Many-to-Many relationship:

  1. Go to Setup > Objects
  2. New Custom Object. Name the object something relevant such as Job Posting. Auto-number is recommended name type
  3. Go to the junction object and create two Master-Detail relationships looking up to the two parent objects

Whenever there is an instance of a Position being associated with an Employment Website for example, create a new Job Posting record and link it to the two relevant records.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s