function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocky.4QRocky.4Q 

Master-Detail Relationship one-to-one

I need the behaviors of the Master-Detail relationship (cascading deletes) but I only want to a one-to-one relationship. What the best way to go about this?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Do you need to restrict user behaviour in some way?  Master detail can be used in a 1-1 way simply by only creating one child object.  If you need to ensure that only one detail is attached, there's a variety of ways to achieve this.  One would be to have a validation rule that errors if the roll up summary (count) of detail records is > 1.  Aside from that you could have a trigger that sets the detail record id into the master and error if this is already populated.

All Answers

bob_buzzardbob_buzzard

Do you need to restrict user behaviour in some way?  Master detail can be used in a 1-1 way simply by only creating one child object.  If you need to ensure that only one detail is attached, there's a variety of ways to achieve this.  One would be to have a validation rule that errors if the roll up summary (count) of detail records is > 1.  Aside from that you could have a trigger that sets the detail record id into the master and error if this is already populated.

This was selected as the best answer
Rocky.4QRocky.4Q

I assumed so, but I wanted to make sure I wasn't missing something. I programmatically create the the relationship, so removing the "new" button from the related list and ensuring that another isn't created in the trigger does the job. Thank You. I really just want to enforce the master-detail cascading deletion, an d this seems to work.