How to change display text of a lookup field in Dynamics CRM 2011/13/15 using JavaScript

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+=\”\”;xmlText+=\”\”;
xmlText+=\”\”;
xmlText+=\”salesorder\”;
xmlText+=\”\”+lookup.getValue()[0].id+\”\”;
xmlText+=\”\”;
xmlText+=\”false\”;
xmlText+=\”\”;
xmlText+=\”ordernumber\”;
xmlText+=\”
\”;
xmlText+=\”\”;
xmlText+=\”\”;
xmlText+=\”
\”;
xmlText+=\”
\”;
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 :


Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

3 thoughts on “How to change display text of a lookup field in Dynamics CRM 2011/13/15 using JavaScript

  1. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

  2. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: