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

Anonymous said...

艾葳酒店經紀公司提供專業的酒店經紀, 酒店上班小姐,八大行業,酒店兼職,傳播妹,或者想要打工兼差打工,兼差,八大行業,酒店兼職,想去酒店上班, 日式酒店,制服酒店,ktv酒店,禮服店,整天穿得水水漂漂的,還是想去制服店日領上班小姐,水水們如果想要擁有打工工作、晚上兼差工作兼差打工假日兼職兼職工作酒店兼差兼差打工兼差日領工作晚上兼差工作酒店工作酒店上班酒店打工兼職兼差兼差工作酒店上班等,想了解酒店相關工作特種行業內容,想兼職工作日領假日兼職兼差打工、或晚班兼職想擁有鋼琴酒吧又有保障的工作嗎???又可以現領請找專業又有保障的艾葳酒店經紀公司!

一般的酒店經紀只會在水水們第一次上班和領薪水時出現而已,對水水們的上班安全一點保障都沒有!艾葳酒店經紀公司的水水們上班時全程媽咪作陪,不需擔心!只提供最優質的酒店上班,酒店上班,酒店打工環境、上班條件給水水們。心動嗎!? 趕快來填寫你的酒店上班履歷表

等相關服務 幫您快速的實現您的夢想~!!

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

Quân Đào said...

dịch vụ dọn dẹp sổ sách kế toán
dịch vụ dọn dẹp sổ sách kế toán tại thái bình
dịch vụ dọn dẹp sổ sách kế toán tại phú thọ
dịch vụ dọn dẹp sổ sách kế toán tại hưng yên
dịch vụ dọn dẹp sổ sách kế toán tại quận hải dương
dịch vụ dọn dẹp sổ sách kế toán tại hải phòng
dịch vụ dọn dẹp sổ sách kế toán tại quận thanh trì
dịch vụ dọn dẹp sổ sách kế toán tại quận hoàng mai
dịch vụ dọn dẹp sổ sách kế toán tại quận hai bà trưng
dịch vụ dọn dẹp sổ sách kế toán tại quận hoàn kiếm
dịch vụ dọn dẹp sổ sách kế toán tại quận từ liêm
dịch vụ dọn dẹp sổ sách kế toán tại quận ba đình
dịch vụ dọn dẹp sổ sách kế toán tại quận tây hồ
dịch vụ dọn dẹp sổ sách kế toán tại quận đống đa
dịch vụ dọn dẹp sổ sách kế toán tại bắc ninh
dịch vụ dọn dẹp sổ sách kế toán tại quận tphcm
dịch vụ dọn dẹp sổ sách kế toán tại quận cầu giấy
dịch vụ dọn dẹp sổ sách kế toán tại quận long biên
dịch vụ dọn dẹp sổ sách kế toán tại quận hà đông
dịch vụ dọn dẹp sổ sách kế toán tại quận thanh xuân
dịch vụ hoàn thuế gtgt