Usually when we select a value in a lookup field it displays the Primary Name Field in the text after selection. However it holds the PrimaryIDField in the background.
While working on a requirement a need arise to display a field in the LookUp display which is not a PrimaryNameField.
Let's consider the Order Lookup field here on Case Form.
Requirement :
Display OrderNo in the display field instead of the OrderName (Which is a PrimaryNameField) in every Order Lookup.
(I have implemented this requirement in CRM 2015)
Before Using Javascript Lookup field Display text will look like below :
Javascript :
Pass Lookup Field as Parameter (I had passed "new_orderno" as fieldname parameter)
Put this Javascript on 'onchange' event of lookup field-
//Change Lookup Display Text
function ChangeLookUpDisplayValue(fieldname)
{
var lookupData = new Array();
var lookupItem= new Object();
var lookup = Xrm.Page.data.entity.attributes.get(fieldname);
if(lookup!=null)
{
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>salesorder</entityName>";
xmlText+="<id>"+lookup.getValue()[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>ordernumber</b:string>";
xmlText+="</a:Columns>";
xmlText+="</columnSet>";
xmlText+="</Retrieve>";
xmlText+="</s:Body>";
xmlText+="</s:Envelope>";
var xHReq = new XMLHttpRequest();
var url=Xrm.Page.context.getClientUrl()+"/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;
Xrm.Page.getAttribute("new_orderno").setValue([{id:lookup.getValue()[0].id,name:displayvalue,entityType:"salesorder"}]);
}
}
}
}
After Putting Above Javascript Lookup display text will look like below :
if the form is saved the display text of lookup automatically changed into old
ReplyDelete