Do you need the value of a lookup? I have written this bit of code to look a lookup value stored on another entity. Like finding the Customer lookup on an Incident (Case).
- ID: The unique id value of the entity’s object
- ENTITY: The name of the entity to lookup
- IDFIELD: The entity’s primary id field name, like accountid, incidentid, etc.
- LOOKUPFIELD: The name of the field you want to lookup, like customerid
- RETURNTYPE: The name of the entity’s return type, like contact or account. If there are multiple possible, then specify “multiple” and it will pass back the type instead of type name. If you specify “value” it will return just the value.
function getEntityLookup(id, entity, idfield, lookupfield, returntype) {
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><soap :Body>" +
"<fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchxml>" +
" <fetch mapping='logical' count='1'>" +
" <entity name='" + entity + "'>" +
" <attribute name='" + lookupfield + "' />" +
" <filter>" +
" <condition attribute='" + idfield + "' operator='eq' value='" + id + "' />" +
" </filter>" +
" </entity>" +
" </fetch>" +
"</fetchxml>" +
"</fetch>" +
"</soap>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
var resultSet = resultXml.text;
resultSet.replace('<', '< ');
resultSet.replace('>', '>');
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
oXmlDoc.loadXML(resultSet);
var result = oXmlDoc.getElementsByTagName(lookupfield);
if (returntype == 'value') return result[0].text;
var lookupItem = new Object();
for (var i = 0; i < result[0].attributes.length; i++) {
var att = result[0].attributes[i];
if (returntype == 'multiple' && att.name == "type") lookupItem.type = att.value;
if (att.name == "name") lookupItem.name = att.value;
}
if (returntype != 'multiple') lookupItem.typename = returntype;
lookupItem.id = result[0].text;
return lookupItem;
}


Thanks for the post. Perhaps I am “thick”… but here is what I am trying to do. I an entity I created (called Goal) I have a lookup field for the Contact Name from the Contact entity. I want to put a Contact name in the Goal record. When the Contact name is selected, I want another field on the Goal entity (Contact phone) to automatically retrieve the Contact phone number from the Contact entity. Can your code do this?
Thanks
That is easier than what this actually does. It looks up a lookup field on another entity, if you just want to lookup a field on a related entity, you could do that with this procedure now that I modified it. I modified the script to take value as a returntype so that you can use it to do what you are trying to do. You would call it with something like the line below. If it doesn’t work, let me know.
getEntityLookup(crmForm.all.contactlookup.DataValue[0].id, ‘contact’, ‘contactid’, ‘phonefieldname’,'value’);
How can I get this to work in CRM 2011? Can you email me with a response?
I will have to look at it more in-depth and get it back to you, but I will add it onto my list of items to look at and post an update to.
Hi Carl,
I am interested in how to do this in CRM online 2011 as well…..or will this code work?
This code should work the way it is with CRM 2011 since the 2007 web services are still available. You could also probably use the REST SDK or a SOAP call to look it up.
does it work with sub-grid in place of lookupfield ???
Damien,
Are you trying to get the value of a lookup in a sub-grid, or are you trying to get the value from the lookup field’s entity, etc? If you could explain in more detail what you are trying to do, I could try and help you accomplish it.