4

What kind of relation should a junction object have with it's parent objects?

What I know from Salesforce documentation: The junction object should have master-detail relationships with the parent objects.

When I check the pricebookentry relationship with product and pricebook, I see this:

enter image description here

Both Product and Price Book are lookups in pricebookentry and pricebookentry is a junction object.

So, doesn't it contradict with the definition of junction object?

I'm confused. Am I missing something? Can I create a junction object using lookups too?

  • Note that both 'lookup' relationships are defined in the schema as cascade delete so no orphans can be created by deleting either a Pricebook2 or a Product2 - so some aspects of master-detail. Also, Product2, Pricebook2, and PricebookEntry don't have ownerIds unlike other junctions you would create. In some ways, using this as an example is a red herring. – cropredy Mar 17 '16 at 17:02
2

I disagree with the "common knowledge" on this issue. I have run into use-cases where using a Lookup relationship instead is appropriate. The junction object is a more flexible concept than people give it credit for. I agree that the following are preferable:

  • Junction should only relate to two objects
  • Junction should never be orphan

However, these requirements are not terribly difficult to satisfy without the use of Master-Detail relationships. For example a Validation Rule can count all the lookups and make sure you have exactly two.

A Concrete Example

Let's say you want to mimic polymorphism in your junction. Consider, for example, if I have one object that I want to relate to five others via many-to-many relationship. Do I create five separate true junctions or one junction-like object? There are valid arguments for each side, but to flat out reject the mock polymorphism because it uses Lookup instead of Master-Detail may not be an option in all orgs (sometimes you are limited to ten custom objects).

3

Junction object is to achieve Many-to-Many relationship model in Salesforce. It means that each record can be associated with many records using the Junction object which keeps the reference of records. So, keep significance of junction object following things are required/considered:-

  1. Junction object must have reference of two different entities (object) on a single record. This is only possible if we create that relationship to master-detail.
    Using Lookup does not guarantee that junction object have reference to two entities. So, many-2-many relationship concept fails here. It Also create chances of orphan records of Junction object.

  2. Master-detail relationship provide concrete advantages over lookup like there will not be any orphan junction object record. Security and sharing settings will be applied appropriately.

  3. There will be consistent result with m-d relationship. Otherwise, making Look-up field required can be considered as junction but there will not be consistency. That junction object will be considered as separate entity. If its parent is deleted then that required field on junction object will be cleared or you may not be allowed to delete parent record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.