Charteris Community Server

Welcome to the Charteris Community
Welcome to Charteris Community Server Sign in | Join | Help
in Search

Alistair Laing's Blog

Dealing with the Microsoft Dynamics CRM 4.0 Lookup Field

If you have used or seen a demonstration of MS CRM 4.0, you will have seen the helpful lookup field - the form field with the search icon beside it. In MS CRM 4.0 it allows you to type a value and the validation will either resolve it to an item it knows about, or indicate that the value is wrong. A helpful combination control that allows more experienced users to proceed quickly through a form, but also provides for them and the less experienced to look up less often used items. Once it has found the item the text changes to a hyperlink so that you can view all of the information on the related entity.

A recent project has required me to push values from an ERP system in to CRM. In functional terms think of the ERP system providing a parts catalogue from production management, and CRM needing to know a specific parts listing from a customer perspective.

Coding against Lookup fields is a bit of a challenge to begin with, for instance the DataValue that they return is not a simple string but an array, so you have to parse it. Similarly in the reverse path, you need to set the values of the control with care. My original posting indicated an approach using information from sample code in the MS CRM 4.0 SDK. I stitched up the code based on what I could find in web postings and the SDK and this was setting the values and resolving them to the lookup.

The problem I later discovered is that rather than the guid of the entity definition, what you should really pass is the guid of the instance of the entity. So even though the value looked ok and resolved ok on the form, clicking through on the hyperlink indicated that it was not a correct relationship to the other entity. Due to this I hunted for an alternative approach and found it via a blog posting to a Microsoft forum posting by Adi Katz in answer to a question on linking a lookup.

The advantage of this approach is that there is no need to look up guids, and that the built in features of the lookup give a visual indication to the user if or not the item has been recognised. A fragment of the JScript follows:

var LookupField = oCRMForm.all.new_producttype;
LookupField .AutoResolve = 1;
LookupField .SetFocus();
ProductDiv = parent.document.all.new_producttype_d.getElementsByTagName("DIV")[0];
ProductDiv.innerText = product;
LookupField.Lookup(true, true, product, true);

The interesting point I would take from above is to note that it gives you an idea of how form fields are translated in to the raw html in the background, it is knowledge of this that informs the above approach. The SetFocus() will also switch the active tab which can annoy some, but it allows the user to see immediately if the value inserted has been matched to an item on the list or not.

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About alistairl

After starting my career as an accountant, I veered off and commenced a career in software development. Over the years I have concentrated on Microsoft technologies, and participated in the associated certification programme, gaining MCSD, MCDBA and MCPS in relevant subjects. I have been part of and led teams in some of the major Internet developments using Microsoft Technologies in Scotland, and now combine development and business analysis work for Charteris. I have broad experience but recent focus around SharePoint and Dynamics CRM 4.0 integration.