Working With Many-To-Many Relationships
Microsoft Dynamics 365 lets you create custom many-to-many relationships between entities. The adapter exposes objects in the adapter object list that represent these custom many-to-many relationships. The adapter object name is the Relationship Entity Name you entered when you created the relationship in Dynamics 365.
The adapter object has three fields: A primary ID for the object and the object IDs for the two entities in the relationship. Adapter objects for these relationship entities support the seek, insert, and delete operations.
Consider a custom many-to-many relationship between account and contact entities. The name of the adapter object is new_account_contact. The fields for this adapter object are:
- new_account_contactid — Primary ID for the adapter object
- accountid — Object ID of the related account
- contactid — Object ID of the related contact
To add a relationship between an account and a contact in Dynamics 365, use a DTS file with an Insert step on the new_account_contactid object. Create data links with DBLOOKUPs to set the object IDs. For example:
The account name is source field 2. Create a formula on the data link of DBLOOKUP(S2, "T", "account", "name", "accountid").
The contact name is source field 4. Create a formula on the data link of DBLOOKUP(S4, "T", "contact", "fullname", "contactid").
To add a relationship between two Dynamics 365 records
- In the Workbench, create a DTS file with an Insert step for the relationship entity object. The adapter sets the primary ID for the object for you.
- Set
the value of the object IDs for the two related records.
The most straightforward way to do this is to have a data link on the object ID fields using the DBLOOKUP() function to derive the ID based on a value from your source data.
Alternatively, you can use one of the following:
- Target variables — Target variables can be used to set the object IDs.
- Auto-foreign key assignments — Placing a seek step before the relationship object step sets the object ID for you.
See also