Skip to content
Groovy 38 – Create M:M Relationship in Groovy

Custom Object Script

I have created a custom object called Campaign_c and then created a M:M relationship with the Contact object.

The intersection object is called CampIntCont:

  • ObjectA is the Source Object: Campaign
  • ObjectB is the Intersection Object: CampIntCont
  • ObjectC is the Target Object: Person

- Field name for the foreign key to the Source Object: ObjectA_Id_Src_ObjectAToObjectB_c

Campaign_Id_Src_Campaign_cToCampIntCont_c

- Field name for the foreign key to the Target Object: ObjectC_Id_Tgt_ObjectCToObjectB_c

Person_Id_Tgt_PersonToCampIntCont_c

You also created a DCL as a picker for contacts to be added in the custom Object Campaign

I called it: ContactPicker_c

Create a Trigger Before Update (to add a contact on update) or Create an Action Link in the custom object Campaign

The same script works in both situations.

Script:

    
        // Get the party Id of the contact to be added from the DCL
        def partyId = ContactPicker_Id_c
        
        // Create new view for the intersection object
        def addContact = newView('CampIntCont_c')
        def addRow = addContact.createRow()
        
        // Set attributes for the source and target objects
        addRow.setAttribute('Campaign_Id_Src_Campaign_cToCampIntCont_c', Id)
        addRow.setAttribute('Person_Id_Tgt_PersonToCampIntCont_c', partyId)
        
        // Insert the new row
        addContact.insertRow(addRow)