Associate A Record To One of Two Entity Types (CRM 2011)
Here’s another old blog I’m republishing. This functionality is now native in CRM 2013 and CRM 2015 as Business rules.
Sometimes, you want to one same entity and associate it to two other entity types. For example, you might have a License that could apply to either an Account or a Contact. You could create two entities – an “Account License” and a “Contact License,” or you could use this simple technique to use one entity that appears to be linked to a Contact or an Account.
- Create your new entity.
- On the Entity form, add three fields:
- A “Two Options” field called “Owned By,” and set option 0 to be Account and 1 to be Contact. Set the default to Account (1).
- A Lookup to a Contact called something like “Owner (Contact)”
- A Lookup to an Account called something like “Owner (Account)”.
- Create a new Web Resource with the following code:
if (Xrm.Page.ui.getFormType() == 1)
// On form create (formType = 1), the system will map either the Contact or Account that it was created from.
// We defaulted the Owned By to assume it’s an Account.
// So if it’s a Contact, change the field.
if (Xrm.Page.data.entity.attributes.get("new_ownercontact").getValue() != null)
// Disable this field in all cases, not just on create.
// Call the below function which will display only Account or Contact to the user.
// Toggles ownership field (contact/account) based on the user input.
var ownedBy = Xrm.Page.data.entity.attributes.get("new_ownedby").getValue(); // Account = 0; Contact = 1
if (ownedBy == 1) // Account
if (ownedBy == 0) // Contact
- In the Form Properties, in the Events tab, add the new Web Resource and set the onLoad() to call the new licenseOnLoad() function.
One drawback to this approach is that if the user creates a License but not from Account or Contact, such as from the FILE tile in the top left, there will be no mapping to indicate Account or Contact ownership, so the License will always be associated to Account. You can enforce this with process or provide a Dialog or Workflow to allow users to fix retroactively.