Description:
In this example we explain that ow to change display text of a Lookup field in Dynamic CRM using JavaScript. Or how to display Lookup Text instead of Default Field using JavaScript in Dynamic 365.or how to display related entity field in Lookup instead of the primary entity field in Dynamic CRM using JavaScript.
Generally, in
Lookup when we open it displayed the primary field in Text after selection but
our requirement was that to display other field in Lookup which is not PrimaryNameField.
Here we
explain the Account lookup field that will by default display Account Name in
Lookup because it is Primary Field Name now we want to display Account Number
instead of the Account Name.
Code:
function ChangeLookUpDisplayValue(fieldname) {
var
lookupData = new Array();
var
lookupItem = new Object();
var
lookup = document.getElementById(fieldname);
if
(lookup != null && lookup.DataValue != null) {
lookupItem.id =
lookup.DataValue[0].id;
lookupItem.typename =
lookup.DataValue[0].typename;
var
displayvalue = '';
var
xmlText = '';
xmlText += "<s:Envelope
xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
xmlText += "<s:Body>";
xmlText += "<Retrieve
xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\"
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
xmlText += "<entityName>account</entityName>";
xmlText += "<id>" + lookup.DataValue[0].id + "</id>";
xmlText += "<columnSet
xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
xmlText += "<a:AllColumns>false</a:AllColumns>";
xmlText += "<a:Columns
xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
xmlText += "<b:string>accountnumber</b:string>";
xmlText += "</a:Columns>";
xmlText += "</columnSet>";
xmlText += "</Retrieve>";
xmlText += "</s:Body>";
xmlText += "</s:Envelope>";
var
xHReq = new XMLHttpRequest();
var url =
Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web";
xHReq.open("POST", url, false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml;
charset=utf-8");
xHReq.setRequestHeader("Accept", "application/xml,
text/xml, */*");
xHReq.send(xmlText);
//
Capture the result.
var
resultXml = xHReq.responseXML; var varray = new Array();
//
Check for errors.
var
errorCount = resultXml.selectNodes('//s:Fault').length;
if
(errorCount != 0) {
var
msg = resultXml.selectSingleNode('//faultstring').nodeTypedValue;
alert(msg);
} else
{
var
result = resultXml.getElementsByTagName("a:KeyValuePairOfstringanyType");
if
(result.length > 0) {
displayvalue =
result[0].childNodes[1].lastChild.text;
}
if
(displayvalue != '') {
lookupItem.name =
displayvalue;
lookupData[0] =
lookupItem;
lookup.DataValue =
lookupData;
}
}
}
}
0 comments:
Post a Comment