Lookup a lookup on another entity using JScript
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;
}
2 Responses to “Lookup a lookup on another entity using JScript”
Comment from Carlton Colter
Time June 11, 2010 at 11:01 pm
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’);
Comment from Todd
Time June 4, 2010 at 8:18 pm
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