Relationships in Salesforce
By: Ankit Sharma On: August 20, 2020
Relationship in Salesforce is used to link objects with each other, so when a user view records, he can also see related data. For example, link a custom object called “Application” to “Candidate” to track applications that are associated with the candidate. You can define different types of relationships by creating custom relationship fields.
In the master-detail relationship, objects are closely linked. It means If we delete master records then detail (Child) records are deleted. The Master-Detail Relationship is used when we want to control the display of detail records based on the master record. Rollup-summary field can be created in a master-detail relationship.
• Deleting a child record moves it to the Recycle Bin and leaves the master record intact, means when a child record is deleted, its master record is not deleted. But deleting a master record also deletes related child records.
• Undeleting a child record restores it, and undeleting a master record also undeletes related child records, means undeleting a master record restores both the master record and its related child records. However, if you delete a child record and later, separately, delete its master record, you cannot undelete the detail record, as it no longer has a master record to relate to.
• By default, records can’t be reparented in master-detail relationships. It means parents of a child record cannot be changed, once it is assigned and saved. However, Administrators can allow child records in master-detail relationships on custom objects to be reparented to different parent records by selecting the Allow reparenting option in the master-detail relationship definition.
• Child records inherit security settings and permissions from the master record. You can’t set permissions on the child record in the master-detail relationship independently.
• The master-detail relationship field (which is the field linking the objects) is required on the page layout of the child records.
• Each custom object can have up to two master-detail relationships and up to 25 total relationships.
A lookup relationship essentially links two objects together so that you can “lookup” one object from the related items on another object. Lookup relationships can be one-to-one or one-to-many. Unlike the Master-Detail relationship, lookup fields are not required by default but we can select a checkbox to make it required.
For Example, the Account to Contact relationship is one-to-many because a single account can have many related contacts.
When you create a lookup relationship, you can set these options:
• Make the lookup field required for saving a record, requiring it on the corresponding page layout as well.
If the lookup field is optional, you can specify one of three behaviors to occur if the lookup record is deleted:
• Clear the value of this field: This is the default. Clearing the field when the field does not have to contain a value from the associated lookup record.
• Don’t allow deletion of the lookup record that’s part of a lookup relationship: If you have dependencies built on the lookup relationship, this option doesn’t allow the lookup record to be deleted.
• Delete this record also: This option is turned off by default and one has to contact Salesforce support to enable this option.
Important Point to Remember here is when creating a new custom lookup field or modifying an existing one, there is a section called ‘What to do if the lookup record is deleted?’ with a few radio buttons as follows:
• Clear the value of this field (this option is unavailable if the field is required)
• Don’t allow deletion of the lookup record that’s part of a lookup relationship
• Delete this record ( this option bypasses security and sharing settings, that’s why it is turned off by default and one has contact Salesforce support to enable this option.)
Sometimes you may notice that this section is missing on some of the setup menus for their lookup fields. The ‘What to do if the lookup record is deleted?’ section will be absent from the configuration of a lookup field if the object which you are looking up to is the same object that the lookup field is on.
You can create a relationship from an object to itself, means self-relationship, but it must be a lookup relationship, and a single record can’t be linked to itself. However, a record can indirectly relate to itself.
Its behavior :
• Parent and child records have their own sharing and security settings in look-up relationships.
• If the parent record in a lookup relationship is deleted, the field history tracking for the child record does not record the deletion. For example, if a parent account is deleted, the Account History related list for the child account does not show the deletion.
To convert a master-detail relationship to a lookup relationship, we have to make sure there are no roll-up summary fields on the master object. If there are rollup summary fields we have to delete those fields to convert it. Deleted Roll-up fields should also be permanently deleted. This can only be done in Salesforce Classic since ‘Deleted Fields’ does not appear in Lightning.
To convert lookup to the master-detail relationship you need to give parent records on all child records. Then only you can convert a lookup relationship to a master-detail relationship if the lookup field in all the records contains a value.