JavaScript – Set optionset values by name
Views (3531)
A simple but hopefully useful JavaScript function. Pass the name of an option set (pick list) and text value to set the value based on the text.
For example;
SetOptionSet(“new_country”, “England”);
// *** Sets the Optionset value based on text
function setOptionSet (fieldName, setText) {
var control = Xrm.Page.getAttribute(fieldName);
if (control.getAttributeType() == 'optionset') {
if (setText == "" || setText == null || setText == undefined) {
control.setValue(null);
}
else {
var controlOpts = control.getOptions();
for (var i = 0; i <= controlOpts.length - 1; i++) {
if (controlOpts[i].text.toLowerCase() == setText.toLowerCase()) {
control.setValue(controlOpts[i].value);
return;
}
}
}
}
else {
alert("Invalid field type or field not found. Field type should be OptionSet");
return;
}
};

Like
Report
*This post is locked for comments