Tuesday, 4 June 2013

Filter Option Set on values in a string (CRM 2011)

Sometimes it could be useful restricting the available options for an Option Set.
I have developed a function that accept as input the CRM option set control and a string.
This string parameter contains all string values that we want in the option set.
For reading simplicity I use to separate them with a character (';' for example) but technically it is not a need: the function searches every option value as a substring of it.
Another way to achieve this goal it could be to work with a list (an array) of accepted values, but I have found much easier and less complex the use of a string as "accepted values container".
 

function filterOptionSetValues(optionSetControl, filterValues) {
if (filterValues) {
var options = optionSetControl.getAttribute().getOptions(); //Get the list of values
optionSetControl.clearOptions(); //Clear Option set
for (var i = 0; i < options.length; i++) {
var find = filterValues.indexOf(options[i].text); //Search the string value in the string
if (find >= 0) {
if (parseInt(options[i].value) > 0)
{
//Build a new option and add to the control
var opt = new Option();
opt.text = options[i].text;
opt.value = options[i].value;
optionSetControl.addOption(opt);
}
}
}
}
}
 


Usage example:
var acceptedValues = "value1;value2;";
filterOptionSetValues(Xrm.Page.getControl("optionSet_name"), acceptedValues);


Hope it can be useful.
Happy CRM coding.


Rate this posting:
{[['']]}

No comments:

Post a Comment