We may often not want to show all the options from an OptionSet in the form.
Here, we can hide options from OptionSet using JavaScript in Dynamic 365 CRM.
Let's see the below scenario.
We have a Job Role field on the Contact form where we have some of the options in OptionSet. We need to hide one of the options from the OptionSet field.
Now, we want to hide the "Dentist" option from OptionSet when a Contact form loads.
Now, with the above information shown, we can go and hide the option “Dentist”.
With the help of JavaScript Code, we can hide the options that we don’t need to display on the form.
Below is the code we need to register the OnLoad of the Contact form and pass the Context as the first parameter.
Next, we need to call the function on OnLoad, as shown in the below screenshot.
JavaScript Code
oContactJobRole = {
hideDentistOption: function (context) {
"use strict"; debugger;
var formContext = context.getFormContext();
var gradeOptionSet = formContext.getControl("new_jobrole");
if (gradeOptionSet !== null) {
gradeOptionSet.removeOption(100000002);
}
}
};
Now, when we load the form, the "Dentist" options are no longer visible.