Home > MS CRM 4.0 > One to one Relationship in MS CRM 4.0

One to one Relationship in MS CRM 4.0


You CANNOT create a One-One relationship in MS CRM 4.0. There is no way you can do that! Humm… Not realy, here is an alternative work around if you desperately need it. The idea is same as we creating multiple relationships between to entities in CRM 3.0, which only support 1 relationship between them. By using two nvarchar fields on form, one to store lookup GUID, the other to store lookup text, converting one of the text fields into a lookup field via Javascript. We then can choose any record in the lookup, even an one-one relationship.

Virtual Lookup CRM 4.0:

crmForm.createVirtualLookup = function(idField, valueField, typecode, typename, isCustomEntity) {
    var iconURL;
    if (isCustomEntity)
        iconURL = "/_imgs/icon.aspx?objectTypeCode=" + typecode + "&iconType=GridIcon&inProduction=1&cache=1";
    else
       
iconURL = "/_imgs/ico_16_" + typecode + ".gif";

    var tmpid = idField.id;
    var tmpid_value = idField.DataValue;
    var tmpid_req = idField.req;
    var tmpid_tabIndex = idField.tabIndex;
    var tmpid_onchange = idField.onchange;
    var tmpid_disabled = idField.Disabled;

    idField.outerHTML = "<td id=’" + tmpid + "_d’>" +
        "<table class=’ms-crm-Lookup’ cellpadding=’0′ cellspacing=’0′ width=’100%’ style=’table-layout:fixed;’>" +
        "<tr><td><div ime-mode=’auto’ class=’ms-crm-Lookup’ tabindex=’" + tmpid_tabIndex + "’></div>" +
        "<label class=’ms-crm-Hidden-NoBehavior’ for=’" + tmpid + "_ledit’></label>" +
        "<input class=’ms-crm-Hidden-NoBehavior’ ime-mode=’auto’ type=’text’ tabindex=’" + tmpid_tabIndex + "’ id=’" + tmpid + "_ledit’ maxlength=’1000’/>" +
        "</td>" +
        "<td width=’25’ CLASS=’Lookup_RenderButton_td’>" +
        "<img src=’/_imgs/btn_off_lookup.gif’ id=’" + tmpid + "’ class=’ms-crm-Lookup’ tabIndex=’" + tmpid_tabIndex + "’ Style=’ime-mode:auto’ resolveemailaddress=’0′ showproperty=’1′ autoresolve=’1′ defaulttype=’0′ lookuptypes=’" + typecode + "’ " +
        "lookuptypenames=’" + typename + ":" + typecode + "’ lookuptypeIcons=’" + iconURL + "’ lookupbrowse=’0′ lookupstyle=’single’ req=’" + tmpid_req + "’ >" +
        "<a href=’#’ onclick=’previousSibling.click();’ tabindex=’-1′></a>" +
        "</td></tr></table></td>";

    idField = document.getElementById(tmpid);
    idField.attachEvent(‘onchange’, tmpid_onchange);
    idField.Disabled = tmpid_disabled;
    idField.aoUnattachedEvents = new Array();
    if (valueField.DataValue != null && valueField.DataValue != "") {
        var luItems = new Array();
        luItems[0] = new LookupControlItem(tmpid_value, typecode, valueField.DataValue);
        idField.DataValue = luItems;
    }
}

Call the function:

crmForm.createVirtualLookup(crmForm.all.new_spouseguid, crmForm.all.spousesname, ‘2’, ‘contact’, false);

Both new_spouseguid and spousesname are nvarchar type. Put spousesname into a hidden tab. The createVirtualLookup function will convert text field new_spouseguid into a contact lookup field. The selected contact GUID will stored in new_spouseguid, the contact name is stored in spousename. Thanks Marwin for helping me on this.

Advertisements
Categories: MS CRM 4.0
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: