1

I want to create a custom object called Groups. I then want to create a M2M Relationship between Groups and Accounts. Then I want to Create a M2M relationship between Groups and Contacts. Is there a way to do this? I have seen lots of documentation on creating Junction objects for a M2M on 2 objects but never 3.

1

The N-way junction object isn't something that Salesforce supports out of the box, but it can be done with a little work and a few compromises.

The big thing here is that Salesforce only allows 2 Master-Detail relationships on a given object. A consequence of having master-detail relationships though is that they must all be populated.

So in an N-way junction object (N >= 2), each record would be required to be related to 2 records. If your 2 M-D relationships are to Group and Account, then your records relating Group and Contact would also still need to be related to an Account.

You could work around that by using Lookup relationships instead, but you'd lose the ability to do rollup summaries (at least without involving triggers in one form or another), and gain an explicit OwnerId field.

If you would go with the lookup relationships approach, you might want to use a validation rule to ensure that 2 of your 3 relationship fields are populated. Something like

OR(
    // !(1 || 2 || 3) is the same as (!1 && !2 && !3)
    // In this case, this disallows all 3 lookups from being populated
    NOT(OR(
        ISBLANK(Lookup_A__c),
        ISBLANK(Lookup_B__c),
        ISBLANK(Lookup_C__c),
    )),

    // This section specifies that at least 2 lookups need to be populated
    // Not really practical for requiring 3 or more lookups be populated, and this
    //   approach will only scale so far
    // It might be a better use of time to enforce this in a trigger
    AND(
        ISBLANK(Lookup_A__c),
        ISBLANK(Lookup_B__c)
    ),
    AND(
        ISBLANK(Lookup_A__c),
        ISBLANK(Lookup_C__c)
    ),
    AND(
        ISBLANK(Lookup_B__c),
        ISBLANK(Lookup_C__c)
    )
)
| improve this answer | |
3

What you're describing is a junction object. A junction object is the third object which represents a many-to-many relationship between two other objects. Your Group object would be a junction object between Account and Contact.

Your objects may have a maximum of two Master-Detail relationships, which is a junction object defining a many-to-many relationship between two objects.

However, you may have additional Lookup relationships to third (and fourth, and so on) objects. You should carefully choose which two objects are your Master-Detail relationships, as it's those which can benefit from functionality like Rollup Summary Fields, inheriting the parents' OWD, and so forth.

| improve this answer | |
  • 3
    ... or if you don't want any access to be inherited on the junction object, they may all be lookup relationships, with no Master-Details to be found anywhere. – Mark Pond Dec 10 '19 at 17:05

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.