Convert
TextBox to DropDown in CRM 2011 Form
Today,
we got a requirement where we don’t know the options to be populated in the
dropdown as they are supposed to get from external data source. So we thought
of converting TextBox to DrowDown with the values from data source in
JavaScript and worked on it.
Here
is the code for it…
//
Conver TextBox to DropDown in CRM 2011
function CreateDropDownList() {
//synchronous
AJAX function to get Xml content from a custom webservice
$.ajax({
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: url + "/**********.asmx/*************",
data: "{'status':'true'}",
beforeSend: function (XMLHttpRequest) {
//Specifying
this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//XMLHttpRequest.setRequestHeader("Content-Type",
"text/xml; charset=utf-8");
},
success: function (XmlHttpRequest) {
//alert(XmlHttpRequest);
msg = XmlHttpRequest.d; //window.JSON.parse(XmlHttpRequest.responseText);
//alert('Response
' + msg);
},
error: function (XmlHttpRequest) {
var error = window.JSON.parse(XmlHttpRequest.responseText);
//alert("Error
: " + XmlHttpRequest.responseText);
}
});
// Check
for the browser
try {
if (window.ActiveXObject) {
//Code
for IE
var Details = msg;
var NDetails = Details.replace(/\\"/g, '\"')
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(NDetails);
}
else {
// code
for Mozilla, firefox, Opera, etc.
var Details = msg;
var NDetails = Details;
xmlDoc =
document.implementation.createDocument("", "", null);
parser = new DOMParser();
xmlDoc = parser.parseFromString(NDetails,
"text/xml");
}
}
catch (e) {
alert(e.message);
}
var lsElements = xmlDoc.getElementsByTagName("Description");
////Erase
the pick list
var pick = document.getElementById("new_source");
var ctName = pick.name;
var ctId = pick.id + '_lst';
var ctClass = "ms-crm-SelectBox";
// Get
the existing, we can use for populating it on the dropdown.
var pVal = Xrm.Page.getAttribute("new_source").getValue();
var parent = document.getElementById('new_source_d');
pick.style.display = "none";
var picklistControl = document.createElement("SELECT");
////Copy
the Text field properties to the picklist
picklistControl.id = ctId;
////
picklistControl.req = textControl.req;
picklistControl.name = ctName;
//// Set
Required Style
picklistControl.className = ctClass;
var usedNames = {};
////Create
a new Option
var option = document.createElement("OPTION");
option.value = 0;
option.innerText = "";
option.text = "";
////Add
the option to the picklist
picklistControl.appendChild(option);
for (vCount = 0; vCount < lsElements.length; vCount++) {
var Source = lsElements.item(vCount).childNodes.item(0).nodeValue;
//// Do
not add duplicate options
var isExist = false;
if (usedNames[Source.toUpperCase()]) {
continue;
}
else {
usedNames[Source.toUpperCase()] =
Source.toUpperCase();
}
////Create
a new Option
option = document.createElement("OPTION");
option.value = vCount + 1;
option.innerText = Source;
option.text = Source;
if (pVal != null && Source != null) {
// Check
if Source and Existing Value is same, just select option set selected property
to True.
option.selected = (trim(Source) ==
trim(pVal));
}
////Add
the option to the picklist
picklistControl.appendChild(option);
}
////append
the picklist to the document
parent.appendChild(picklistControl);
}
//
--
Happy
CRM’ing,
Gopinath
is this supported?
ReplyDeleteand is it supported in crm 2016/5?
It is not the Supported way, I have used in CRM 2011.
ReplyDelete