1. We need to create the option set that we'll be converting
>>Contact>>Customize Tab>> Customize Form
2. Create a hidden field, this will hold the values of the user selects. This field is of type Multiple Lines of Text
3. Create a new Web Resource (Jscript) with the following code:
Code: Select all
// JScript source code
var var_new_picklistacc = 'new_year';
var var_new_picklistvalueacc = 'new_yearvalue';
//Method to convert picklist to multi select picklist OnLoad of entity
function Onload_Entity()
{
var PL = document.all.new_year;
var PLV = document.all.new_yearvalue;
Xrm.Page.getControl('new_yearvalue').setVisible(false);
ConvertToMultiSelectext(PL,PLV,"80");
}
// Save the selected text, this field can also be used in Advanced Find
function OnSave_Entity()
{
//Update >> Provide picklist schema name
var PL = document.all.new_year; OnSave(PL, var_new_picklistvalueacc);
}
///////Supported functions
function ConvertToMultiSelectext(p_PL,p_PLV,ht)
{
// Create a DIV container
var addDiv = document.createElement("<div style='overflow-y:auto; height:" + ht + "px; border:1px #6699cc solid; background-color:#ffffff;'/>");
p_PL.parentNode.appendChild(addDiv);
// Initialise checkbox controls
for( var i = 0; i < p_PL.length; i++ )
{
var pOption = p_PL.options[i];
if(pOption.text!="") {
if( !IsChecked( pOption.text , p_PL, p_PLV) )
var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />" );
else
var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />" );
var addLabel = document.createElement( "<label />");
addLabel.innerText = pOption.text;
var addBr = document.createElement( "<br/>"); //it's a 'br' flag
p_PL.nextSibling.appendChild(addInput);
p_PL.nextSibling.appendChild(addLabel);
p_PL.nextSibling.appendChild(addBr);
}
}
}
// Check if it is selected
function IsChecked( pText , PL, PLV)
{
var PLV1 = PLV.innerText;
if(PLV1 != "")
{
var PLVT = PLV1.split(";");
for( var i = 0; i < PLVT.length; i++ )
{
if( PLVT[i] == pText )
return true;
}
}
return false;
}
function OnSave(OS, var_new_optionsetvalue) {
var getInput = OS.nextSibling.getElementsByTagName("input");
var result = '';
for (var i = 0; i < getInput.length; i++) {
if (getInput[i].checked) {
result += getInput[i].nextSibling.innerText + ";";
}
}
//save value
control = Xrm.Page.getControl(var_new_optionsetvalue);
attribute = control.getAttribute();
attribute.setValue(result);
}
>>On Load
>>On Save
5. Save and Publish your changes. We're done! When you create a new contact, the year field should be like this.