Home | Contact Us

Powered by Blogger

Saturday, March 31, 2007

One-to-Many Relationships - Part II

This is an update to my previous post with additional instructions for maintaining the lookup value's name on the edit form.

CAUTION: Unsupported customizations ahead -- use at your own risk!

Creating one-to-many relationship in Microsoft CRM can usually be accomplished through standard configuration using the tools included in the product. However, relationships between two system entities not previously created by the system cannot be added using the tools. Instead, a custom attribute must be added to the entity via the Export / Import XML customization file.

The following steps describe the process for creating a one-to-many relationship via custom attribute

Creating a custom attribute

  1. Export the customizations for the entity to be on the “child” side of the one-to-many relationship
  2. Add a new attribute with a physical name describing the primary key of the “parent” entity
  3. Set the element IsCustomField equal to 1
  4. Set the element ReferencedEntityObjectTypeCode to the object type code of the “parent” entity. For example:
    <attribute PhysicalName="FieldId">
    <Type>lookup</Type>
    <ValidForCreateApi>1</ValidForCreateApi>
    <ValidForUpdateApi>1</ValidForUpdateApi>
    <ValidForReadApi>1</ValidForReadApi>
    <IsCustomField>1</IsCustomField>
    <ReferencedEntityObjectTypeCode>1</ReferencedEntityObjectTypeCode>
    <DisplayMask>ValidForAdvancedFind|ValidForForm|ValidForGrid</DisplayMask>
    <Description />
    </attribute>

  5. Import the customizations and publish the changes
  6. Add a second attribute via the customizations form for storing the name of the selected lookup. This field will not be visible to the user, but is needed to properly display the lookup on the form.
  7. Add both attributes to the edit form
  8. Add the following JavaScript to the OnLoad event of the form:

    Note: This sample assumes the lookup attribute is named FieldId and the name attribute is named FieldIdName
    /* Hide the name field */
    crmForm.all.fieldidname_c.style.display = 'none';
    crmForm.all.fieldidname_d.style.display = 'none';

    /* Retrieve the lookup control */
    var o = crmForm.all.fieldid;
    var a = o.getLookupField().getElementsByTagName("SPAN");

    /* Update the name if a lookup was found */
    if (a && a.length > 0)
    {
    /* Retrieve the name value */
    var name = crmForm.all.fieldidname.DataValue;

    /* Update the lookup with the name value */
    a[0].innerHTML = a[0].innerHTML.replace(/>.*$/, '>' + HtmlEncode(name));

    /* Reset the IsDirty flag by setting the default value */
    o.DefaultValue = o.DataValue;
    }
  9. Add the following JavaScript to the OnChange event of the lookup:
    /* Retrieve the lookup value */
    var items = crmForm.all.fieldid.DataValue;
    var o = (items == null items.length == 0) ? null : items[0];

    /* Update the name field */
    crmForm.all.fieldidname.DataValue = (o == null) ? '' : o.name;

Labels: ,

Copyright © 2007 AdvantageWorks Software Group, LLC. All rights reserved.