Convert text field to picklist in MS CRM

October 18, 2010 by  

This function below describes how you can convert a text field into a picklist.This original script was developed by MS CRM 4.0 – Unleashed. I made a few changes to it so you can now save a picklist value while displaying the text. If no text array is passed than the values will be shown.

gConvertTextToPickList=function (controlId, valuearray, textarray) {

 Description:   Funcion to convert a nvarchar (text) field into a picklist
 Calls:   AddPickListValues()
 Returns:   nothing
 Example:  var arrValues = ['1','2','3'];
    var arrText = ['One','Two','Three'];
    gConvertTextToPickList('<your_textfield>',arrValues, arrText);

 //Referance the Text field
 var textControl = document.getElementById( controlId );
 if (!textControl) {return;}

 if (!textarray || (valuearray.length != textarray.length)) {
  textarray = valuearray;

 //Create a new Picklist (SELECT) control
 var picklistControl = document.createElement( "SELECT" );
 //Copy the Text field properties to the picklist =;
 picklistControl.req = textControl.req;

 //Set Required Style,  add the picklist values 
 picklistControl.className = "ms-crm-SelectBox "; //"selectBox " v3.0
 AddPickListValues( picklistControl, valuearray, textarray);

 //load the picklist selected value from the text field (saved) datavalue
 picklistControl.value = textControl.DataValue;
 textControl.parentElement.appendChild( picklistControl );  //append the picklist to the document
 textControl.parentElement.removeChild( textControl );   //remove the text field , we don't need it anymore.

 function AddPickListValues(picklistControl, valuearray, textarray) {

  for( var i = 0 ; i < valuearray.length; i++ ){
   //Create a new Option
   var option = document.createElement( "OPTION" );
   option.value = option.innerText = valuearray[i];
   option.text = option.innerText = textarray[i];
   picklistControl.appendChild( option );       //Add the option to the picklist

Related Posts:
Convert a text field to a button on a form in MS CRM
Add a helper / lookup button to a text field


You must be logged in to post a comment.