Saturday, August 16, 2008

Retrieve Lookup Information

Let’s assume that you're interested in getting the account number from the parent account lookup on the account entity. Basically, the supported way of doing that is binding to the parentaccountid onchange event, extracting the GUID ( crmForm.all.parentaccountid[0].id) or Name (crmForm.all.parentaccountid[0].name) and using that in an Ajax call to retrieve the account number. The problem with that is you need to make yet another call to the server.
There is another way, unsupported though, which uses the items array that is exposed by the lookup. Here is the complete code example:




function OnCrmPageLoad() {
crmForm.all.parentaccountid.attachEvent("onchange",OnParentAccountChange);
}

function OnPatentAccountChange() {
var parentAccount = crmForm.all.parentaccountid;
if( parentAccount.DataValue != null )
{
var lookupColumns = parentAccount.items[0].keyValues;
alert( lookupColumns.accountnumber.value);
//alert( lookupColumns.<Attribute Schema Name>.value);
}
}

OnCrmPageLoad();




The lookupColumns expose the entire column set you see on the lookup dialog by name.

2 comments:

Unknown said...

Hello Adi,
Thanks for the trick... I have tested and it is working on the on change event. I would like to use it also on the onload event.
I am in fact copying the acct number to a field, so when I am loading the page I need to reload my account number (if it has changed). Is it possible?

Adi Katz said...

Hi moonjo,

This only a “trick” as you put it and will work only if you open the lookup dialog.

You should refer to the http://mscrm4ever.blogspot.com/2008/09/ajax-using-fetch-message.html post to get the account number on the “onload” event.