Microsoft Dynamics CRM 2011 doesn't have multi-select picklist
controls available. The standard CRM picklist can only save one value in
the database and it is not easy to extend this functionality. In
addition, you have to deal with the Advanced Find Feature.
Original code came from Jim Wang (http://blogs.msdn.com/b/crm/archive/2009/03/31/crm-4-0-checkbox-style-mu...) but needed customization to work for MCRM 2011
Changes to original code:
var Name = Xrm.Page.data.entity.attributes.get("new_areaaccesstext");
Name.setValue(PLV.value);
- I added an "onClick()" event to build the string of values each time a checkbox is checked.
- The original code did not save it's values into the database when
the "Save" button was clicked, so I added 2 additional lines of code to
Set the values in the textbox and allow MCRM to save the data.
The script below will draw a checkbox style multi-select picklist
control on the CRM form, and then get options from the real picklist
attribute.
"Settings", "Customization", "Customize the System". Click "Web Resource" and then "New"
Type in the name of the Web Resource, Display Name...
Select "Script (JScript)" and choose the Language.
Click the Text Editor button and paste the code below to the source area on the form.
Click "OK" then "Save and Close"
Go to "Settings", "Customization", "Customize the System". Expand "Entity" then expand "Account" and click "Forms"
Select the form where you added the picklist and textbox. And click "Form Properties" button
Add the new Web Resource to the Library by clicking the "Add" button, then select the new web Resource and click "OK"
In the Event Handlers section click "Add", select the Library file
and enter the Function name (In our example: "AccountOnLoad") and click
"OK" and "OK" again
- Controls - Create a picklist and populate it with values & a
textbox to store the string of values selected from the picklist
- Put the picklist on the form where you want the new Multi Select Picklist to show up
- Create a web Resource:
- Add the event to the form:
- Save and Publish All Customizations
Here is the Sample code - You'll need to change the values new_areaaccess and new_areaaccesstext on lines 4, 5, 50, 51, and 63.
function AccountOnLoad ()
{
// PL - the picklist attribute; PLV - used to save selected picklist values
var PL = crmForm.all.new_areaaccess; //CREATE NEW PICKLIST
var PLV = crmForm.all.new_areaaccesstext; //CREATE NEW TEXT FIELD TO STORE STRING
PL.style.display = "none"; //HIDES THE CONTROL
PLV.style.display = "none"; //HIDES THE CONTROL
// Create a DIV container
var addDiv = document.createElement("");
PL.parentNode.appendChild(addDiv);
// Initialise checkbox controls
for( var i = 1; i < PL.options.length; i++ )
{
var pOption = PL.options[i];
if( !IsChecked( pOption.text ) )
var addInput = document.createElement("" );
else
var addInput = document.createElement("" );
var addLabel = document.createElement( "");
addLabel.innerText = pOption.text;
var addBr = document.createElement( "
"); //it's a 'br' flag
PL.nextSibling.appendChild(addInput);
PL.nextSibling.appendChild(addLabel);
PL.nextSibling.appendChild(addBr);
}
// Check if it is selected
function IsChecked( pText )
{
if(PLV.value != "")
{
var PLVT = PLV.value.split("||");
for( var i = 0; i < PLVT.length; i++ )
{
if( PLVT[i] == pText )
return true;
}
}
return false;
}
}
function CallOnChangeEvent()
{
var PL = crmForm.all.new_areaaccess;
var PLV = crmForm.all.new_areaaccesstext;
PLV.value = "";
var getInput = PL.nextSibling.getElementsByTagName("input");
for( var i = 0; i < getInput.length; i++ )
{
if( getInput[i].checked)
{
PLV.value += getInput[i].nextSibling.innerText + "||";
}
}
//MUST DO THIS TO TRIGGER A SAVE EVENT
var Name = Xrm.Page.data.entity.attributes.get("new_areaaccesstext");
Name.setValue(PLV.value);
}