11 December 2008

CRM 4.0 Get attribute value from entity's GUID using JScript

Recently I have been asked many times about how to get attribute value from entity's GUID using JScript?
The following code demonstrate how to get a user's internalemailaddress by giving user's GUID.
*It has been modified to support multi-tenent deployment.

alert(GetAttributeValueFromID("systemuser", "09DF2AB7-E16D-DD11-88F3-0003FF884968", "internalemailaddress", "systemuserid"));

function GetAttributeValueFromID(sEntityName, sGUID, sAttributeName, sID)
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>"+sEntityName+"</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>"+sAttributeName+"</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:PageInfo>" +
" <q1:PageNumber>1</q1:PageNumber>" +
" <q1:Count>1</q1:Count>" +
" </q1:PageInfo>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>"+sID+"</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">"+sGUID+"</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

// retrieve response and find attribute value
var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName);
if (result == null)
return "";
return result.text;


Anonymous said...

Very good Jim. I too get questions like this all the time and you have created an excellent solution.


Anonymous said...

Thanks for sharing this- it was perfect timing for me.

Oz said...

Dear JIM,

I am a bit new to CRM customization. I have an issue which is the cause to a lot of hiccups in my customisation. In the account section when I select the "parent account" lookup it always selects the "Account name", instead my client wants account number to be appearing in the lookup. Is it possible to change the attribute? Because it always pulls the primary attribute. I guess I have to write a onload script to change the attribute. Can you help me with it?

Anonymous said...

Excellent bit of code. However I seem to have a problem with it when running on a system with multiple organisations. I can run it OK on the default org, but if run on a different org it does not find the GUID. If I pass it a GUID from the default org it will return a value!!!! even though I am running the code from a different org. Do you have any ideas on this.

Oz said...


Can you pls tell me how to multiselect in a lookup?
I tried visiting the below link but its giving me an error.

Pls do let me know.


Jim Wang said...

Thanks for your comments, it has been modified to support multi-tenent deployment.;-)

Special thanks for MVP Michael Hoehne and his great tool!

Unknown said...

Jim, this is actually what I need..

However, I am not really a programmer so I dont know what I need to change on the script to make it work.

What I'm trying to accomplish is to auto fill the address when a user select an account thru a lookup field.

I hope you can help me.

Thank you

Anonymous said...

Great script, just what I needed.

One question though: if the field I'm getting value from is a picklist it returns the selected items value (ie 2 if the second option is selected) - how can I get the text?

Anonymous said...

Gregory: I had the same question as you and here's the answer I got on the CRM forums:

Replace the line that reads the content of a single node to a line that reads the value of the name attribute of the particular node, as follows:

var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName);


var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName).getAttribute("name");

This is due to the fact that the selected text is returned as the name attribute of the XML element, and not as its inner text.

Unknown said...

Jim, great post!

I'm using it for a custom asp.net page, but not very succesfull! I get an "object expected" on generateauthenticationheader() function. Is this because I'm not running in a CRM context?

The code works fine in an OnLoad event of the form within CRM, but not in the custom asp.net page in an IFRAME in that CRM-form.

Any ideas?


Ravi said...

Hi Jim,
I created a n:n relationship, here what I want is the records which are associated should not be available in the availalbe records of the look up dialog. It would be great if you help me on this

Unknown said...

Like Bert-Jan, I keep getting an "object expected" error on the GenerateAuthenticationHeader() line of the script.
I've successfully used this script elsewhere but am now trying to open a new page from CRM and am getting this error.

Unknown said...

hey, I get the same error as Bert-Jan and Laurie - "Object Expected" !

does anyone have a solution???


Joeri said...

Will this one work with CRM 2011 as well, or will we need to update the code ?

Unknown said...

Keep up the fantastic piece of work, I read few blog posts on this web site and I believe that your site is real interesting and has lots of great information. ERP Software in Mumbai || System Software || CRM Software in Mumbai || MLM Software

Unknown said...

I really appreciate spending some time to talk about that, I believe firmly regarding this and so really enjoy understanding more about this kind of subject.This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this. CRM Software || MLM Software in Mumbai || ERP Software || System Software in Mumbai