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.

Here’s how:

  1. Create your new entity.
  2. On the Entity form, add three fields:
    1. 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).
    2. A Lookup to a Contact called something like “Owner (Contact)”
    3. A Lookup to an Account called something like “Owner (Account)”.
  3. Create a new Web Resource with the following code:

    [code lang=”js”]
    function licenseOnLoad()
    {
    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)
    {
    Xrm.Page.data.entity.attributes.get(“new_ownedby”).setValue(0);
    }
    }
    // Disable this field in all cases, not just on create.
    Xrm.Page.ui.controls.get("new_ownedby").setDisabled(false);
    // Call the below function which will display only Account or Contact to the user.
    accountContactToggle();
    }

    function accountContactToggle()
    {
    // 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
    {
    Xrm.Page.ui.controls.get("new_ownercontact").setVisible(false);
    Xrm.Page.ui.controls.get("new_owneraccount").setVisible(true);
    }
    if (ownedBy == 0) // Contact
    {
    Xrm.Page.ui.controls.get("new_ownercontact").setVisible(true);
    Xrm.Page.ui.controls.get("new_owneraccount").setVisible(false);
    }
    }
    [/code]

  4. In the Form Properties, in the Events tab, add the new Web Resource and set the onLoad() to call the new licenseOnLoad() function.

As this is jScript, if you are deploying on mobile platforms, be careful.

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.

Comments

Popular posts from this blog

Setting a DateTime to a Weekday in a Formula

"Disjunctions not supported" - Why Custom Metadata and Flow Don't Mix

Update Knowledge Articles in Bulk