I have small doubt in choosing between look up field and master detail field.
I have a custom object say cObj1 which should always be related to some other object say cObj2. So I can opt for universally required look up field or a master detail relation. So what should be chosen and under what basis the choice is made ?If I make a universally required lookup field then it not at all allow deleting the parent object.On the other hand in case of md relation deleting the parent object deletes all the related child object. Is it is something that should be kept in mind before making the choice ?
Also how to make the choice if we have more such relationship required like there are 2 more objects cObj3 and cObj4 with which cObj1 should always be related.
3 Answers
Primary points in choosing MDR over lookup should be:
- Does it make sense? Think about structure. What do the objects represent? When you insert a cObj2 does it depends on the related cObj1?
- Roll-up summary. More often than not this is the cornerstone of my decision. Will you need (at any moment) to count, sum or loop through the child object?
- Deleting childs. If a master record is deleted, all childs go along with it. Does it seems right for that relationship?
- Security. In a MDR, the child sharing settings are inherited from the parent.
-
1Also, and very important, is that in a M/D relationship the security and ownership of the child record is inherited from the parent. Commented Nov 26, 2015 at 17:44
-
If you require more than two of such relationships your choice is quite limited - you can have only two master-detail relationships on a object. Generally I'd say : go with lookup - it can be changed into master-detail later, but not the other way around.
EDIT: Here is a full story and considerations whether to go with lookup or master https://help.salesforce.com/HTViewHelpDoc?id=relationships_considerations.htm
-
2From the Salesforce documentation: Considerations for Relationships Converting Relationships You can convert a master-detail relationship to a lookup relationship as long as no roll-up summary fields exist on the master object. You can convert a lookup relationship to a master-detail relationship, but only if the lookup field in all records contains a value. Commented Oct 8, 2015 at 13:09
I have a custom object say cObj1 which should always be related to some other object say cObj2.
If this is the case, and it is a one-to-one case, I would go with a Master-Detail Relationship. The MDR will enforce that each child object MUST have the parent relationship set. This is a tight coupling.
The Look-up Relationship is loose in that it is not required when creating the object. You can also have several Look-up relationships and only use a subset of these relationships (0, 1, some, all).
Note that if you try to put two MDR fields in an object, BOTH must be assigned a valid parent when creating the object in Salesforce (more specifically, inserting the object).
If I make a universally required lookup field then it not at all allow deleting the parent object.On the other hand in case of md relation deleting the parent object deletes all the related child object. Is it is something that should be kept in mind before making the choice ?
These are two different scenarios, but, yes, you should keep these in mind when deciding depending on how much data you want to stay intact and for what reasons in the event that objects/records need to be deleted.
On the one hand, if you require a lookup field, it should make sense that the parent cannot be deleted as this could cause indeterminate results (most likely and error) when using the child object, unless you have a mechanism to remove the Look-up Relationship value from the child object and/or reassign it elsewhere. This is simply a safety mechanism to avoid unwanted errors and results when manipulating the objects.
On the other hand, if you use a Master-Detail Relationship, since this is a tight coupling of parent to child, the child MUST be reassigned to a different parent object/record (assuming you configured the relationship to allow this), or else deletion of the parent will cause a recursive deletion of children still associated to it. This could lead to a lot of data loss if a user were to haphazardly delete objects without knowing the relationship consequences.
Also how to make the choice if we have more such relationship required like there are 2 more objects cObj3 and cObj4 with which cObj1 should always be related.
If it is 2 and it is required, you could still do a Master-Detail Relationship, keeping in mind what I laid out above. If it is more, then you will have to use the Look-up relationship for anything beyond 2. Note that you can use BOTH MDR and Look-up, just be sure you know the tight coupling consequences and requirements of each when picking which object to point where.
In my opinion, it can be cumbersome and confusing to create a lot of relationships in one object to many parents. I would suggest having an over-arching Main object and having child objects be simple with one relationship to the Main object. However, I'm not sure what your use-case is.
-
Please clarify your line - "If this is the case, and it is a one-to-one case". What do you mean by one to one ? Are you saying one to one relation ? Commented Apr 23, 2015 at 6:48
-
Yes, a one-to-one relation for the child. The parent may still have a one-to-many relation with other objects, but the relation is strictly made from the child side of the relation anyways. You could enforce a one-to-one relation by having a Roll-up field in the parent and counting the number of child objects of that type and then making a Verification rule that throws an error if there is more than one child counted in that roll-up field.– JeffyBCommented Apr 23, 2015 at 14:13
-
I find your explanation unnecessary complicated. I don't want to achieve one to one relation or in other words unique md or look up relation. I just want to conceptualize when to use what and what will be the consequences if used the other one rather than the ideal one ! Commented Apr 24, 2015 at 6:46