Frequently, there is a desire to selectively display options within an OptionSet on a form. In such cases, JavaScript can be utilized to conceal specific options in Dynamics 365 CRM.
Consider the following scenario.
On the Contact form, there is a Job Role field containing various options within an OptionSet. We aim to conceal a specific option from the OptionSet field.
Presently, the objective is to conceal the "Dentist" option within the OptionSet when the Contact form is loaded.
Having presented the preceding information, we can proceed to conceal the "Dentist" option.
Using JavaScript code, we can hide specific options that are not intended for display on the form.
The following code needs to be registered on the Contact form's OnLoad event, with the context passed as the first parameter.
Following that, it is necessary to invoke the function on the OnLoad event, as illustrated in the screenshot below.
JavaScript
contactJobRole = {
hideDentistOption: function (context) {
"use strict";
debugger;
var formContext = context.getFormContext();
var gradeOptionSet = formContext.getControl("new_jobrole");
if (gradeOptionSet !== null) {
gradeOptionSet.removeOption(100000002);
}
}
};
Upon loading the form, the "Dentist" option is now hidden from view.