Object relationships are particular fields that connect two objects in Salesforce. Understanding these relationships is crucial to effectively managing your data and improving your workflow. In this blog post, we’ll explore the two main types of object relationships in Salesforce and everything you need to know about them.
Types of Object Relationships in Salesforce
There are two main types of object relationships in Salesforce: Lookup and Master Detail.
Salesforce Lookup Relationships
A lookup relationship is a way to link two objects together to retrieve one object from the related items on another object. Lookup relationships can be one-to-one or one-to-many. Objects in lookup relationships usually function as independent entities and have tabs in the user interface. However, they do not support sharing or roll-up summary fields.
When you create a lookup relationship, you can set these options:
Required
Make the lookup field required for saving a record, requiring it on the corresponding page layout as well.
What to do if the lookup record is deleted?
If the lookup field is optional, you can specify one of these two behaviors to occur if the lookup record is deleted:
Clear the value of this field.
This option is for clearing the field when it doesn’t have to contain a value from the associated lookup record. However, it can’t be selected if the field is required. This is the default option.
Don’t allow deletion of the lookup record that’s part of a lookup relationship.
Suppose you have dependencies built on the lookup relationship, such as a workflow rule. In that case, this option prevents the lookup record from being deleted.
Delete this record, also.
If a custom object contains a lookup relationship, the option to completely delete related data is available. However, this option is not available if the lookup relationship is contained by a standard object. The lookup object can be either standard or custom. This option should be chosen when the lookup field and its associated record are tightly coupled, and you want to delete them. When you choose to delete the record, a cascade delete may occur. Cascade-delete bypasses security and sharing settings, which means users can delete records even if they don’t have access to them as long as the target lookup record is deleted. This option is disabled by default to prevent accidental deletion of records. If you want to enable it, don’t hesitate to contact Salesforce.
Cascade-delete and its related options are unavailable for lookup relationships to business hours, network, lead, price book, product, or user objects.
NOTE
When deleting a record, it is essential to note that if it has child records, it cannot be deleted unless moved to the Recycle Bin (Soft Delete). However, if all the child records of a parent record are already in the Recycle Bin, the parent record can be deleted. Please note that any child records in the Recycle Bin will also be permanently deleted.
When you establish a lookup relationship, you can add a lookup field to the page layouts for that object and create a related list on the associated object’s page layouts. For instance, if you have a custom object named “PTO Requests” and want your users to link a PTO request with the employee who submitted it, you should create a lookup relationship from the “PTO Request” custom object with the user object.
Suppose the parent record in a lookup relationship is deleted. In that case, the field history tracking for the child record doesn’t record the deletion. For example, if a parent account is deleted, the Account History-related list for the child account doesn’t show the deletion.
Suppose you are trying to delete an object or record in a lookup relationship, and the combined number of records between the two linked objects is more than 100,000. In that case, you won’t be able to do so. In such cases, you need to first delete an appropriate number of its child records before deleting the object or record in a lookup relationship.
Also, remember that when you delete an object used by a lookup field, you must also delete the field. You can use the Metadata API with a delete manifest that uses purge delete to delete both the object and the field or use the Setup in the UI to delete the field first. The object can only be deleted if you delete the field first.
Salesforce Master-Detail Relationships
A master-detail relationship is an association between two objects in which one object is designated as the master and the other as the detail. The master object controls certain aspects of the detail object, such as who can view the detailed data. It’s important to note that the detail object cannot function independently and relies highly on the master object. If a record associated with the master object is deleted, all the corresponding detail records will also be deleted. When creating a master-detail relationship, it’s necessary to create the relationship field on the detail object.
When you delete a detailed record, it will be moved to the Recycle Bin while the master record remains intact. Deleting a master record, on the other hand, will delete related detail and sub-detail records. Suppose you need to restore a deleted record. In that case, undeleting a detailed record will recover it, while undeleting a master record will restore related detail and sub-detail records. However, if you delete a detailed record and later delete its master record separately, the detailed record will remain in the Recycle Bin.
Child records in master-detail relationships on custom objects can’t be reparented to different parent records by default. However, administrators can enable this feature by selecting the Allow Reparenting option in the master-detail relationship definition.
The “Owner” field is not available on the detail records. It is automatically set to the master record’s owner. Custom objects on the detail side of a master-detail relationship cannot have sharing rules, manual sharing, or queues, as these features require the “Owner” field.
Security settings and permissions for detail records are inherited from the master record. It is not possible to set permissions on the detail record independently.
The master-detail relationship field (which links the objects) is required on the page layout of the detail records. As a best practice, stay within 10,000 child records for a master-detail relationship.
Each custom object can have up to two master-detail relationships and up to 40 total relationships.
Modifying the Related To field is impossible once you’ve saved the relationship. A profile or permission set can have an entity, like an Account, linked to another entity through a master-detail relationship. Suppose the child entity has specific permissions that the parent entity should have. In that case, it means there’s a broken permission dependency. Salesforce automatically updates the parent entity to fix this issue when you save the profile or permission set for the first time.
Aspect | Lookup Relationship | Master-Detail Relationship |
Child Dependency | Child records can exist without a parent record. | Child records cannot exist without a parent record. |
Cascade Deletion | No automatic deletion of child records when the parent is deleted. | Automatic deletion of child records when the parent is deleted. |
Roll-up Summary Fields | Roll-up summary fields are not available. | Roll-up summary fields are available. |
Security and Access | Security is controlled through separate sharing settings. | Security is controlled through the parent’s sharing settings. |
Limit | The relationship limit for an object is determined by the total number of lookup and master-detail relationship fields allowed. The default limit is 40, while the maximum hard limit is 50. | two (2) is the maximum allowed. |
Dependency in Reporting | Less dependency, lookup fields can be null. | More dependency, the master record must exist. |
Sharing Settings | It can have different sharing settings than the parent. | Inherits sharing settings from the parent. |
Standard Object | The standard object can be on the detail side of a custom object. | The standard object can be on the detail side of a custom object. |
Salesforce Hierarchical Relationships
Hierarchical relationships are only available on the user object and can be used to create a management chain between users.
Salesforce Roll-Up Summary Fields
Roll-up summary fields calculate values from related records present in a related list. These fields automatically display a value on a master record based on the values in a detailed record. However, it is essential to note that these detailed records should be directly related to the master record through a master-detail relationship.
Roll-up summary fields allow various types of calculations to be performed. For instance, you can easily count the number of detail records related to a master record or calculate the sum, minimum, or maximum value of a field in the detail records.
There Are A Few Different Types Of Summaries You Can Use.
Count
This function calculates the total number of records associated with the details side of a Master-Detail Relationship.
Sum
Totals the values in the field you select in the field to aggregate the option. Only number, currency, and percentage fields are available.
Min
Displays the lowest value of the field you select in the field to aggregate option for all directly related records. Only number, currency, percent, date, and DateTime fields are available.
Max
Displays the highest value of the field you select in the field to aggregate options for all directly related records. Only number, currency, percent, date, and DateTime fields are available.
Best Practices for Object Relationships
Here are some best practices to keep in mind when working with object relationships:
- Use lookup relationships when objects are only related in some cases.
- Use master-detail relationships when the detail object is always related to a master object.
- Be careful when deleting records in a master-detail relationship; all related detail records will also be deleted.
- Use roll-up summary fields to calculate aggregate values from child records in a master-detail relationship.
Takeaways
- Object relationships are essential fields that connect two objects in Salesforce.
- There are two main types of object relationships in Salesforce: Lookup and Master Detail.
- Lookup relationships link two objects to retrieve one object from the related items on another. In contrast, Master-Detail relationships designate one object as the master and the other as the detail, with the detail object relying heavily on the master object.
- When creating a lookup relationship, you can set options such as whether the lookup field is required, what to do if the lookup record is deleted, and whether to delete the record along with its associated lookup record.
- When creating a master-detail relationship, the relationship field on the detail object must be made. If a record associated with the master object is deleted, all the corresponding detail records will also be deleted.
- It’s important to note that a record with child records can only be deleted if moved to the Recycle Bin. If all the child records of a parent record are already in the Recycle Bin, the parent record can be deleted.
Object relationships are essential for organizing your data and improving your Salesforce workflow. Understanding the differences between lookup and master-detail relationships is crucial for effectively managing your data. By following the best practices outlined in this post, you can ensure that your Salesforce org is set up for success.