Hi William ,
Above most of the answers are correct but unfortunately in your scenario will not work. Few things you need to take care -
First -
You wrote
Xrm.Page.getControl("publisher_greenfolderreview").setVisible(false);
I can understand you wrote here web resource name with publisher name , but to get the control from forms you should put name of the webresource in the forms -for example WebResource_NameofWebResourcewithoutpublishername.
You can get the name by double click on web-resource property.
Second -
As you you mentioned you have register the function on save and on load. But for show , hide you dont need to trigger any code in the on-save method. As on-save method actually while saving , you cant see any chagnes during on-save , once forms is save its automatically call on-load method. So in your case trigger JS function in the form-onload and the optionset field onchange event.
Third -
When you are doing show/Hide web-resource controls directly by getting control name (using web resource name) , if the web resource is not render in the page initially , let say you open a existing record with optionset value which does not satisfy condition to show webresource , in that case webresource will not render. Now you are calling setvisible(true) in that case webresource will not show without reopen or reload the page. For that you need to embed the webresource into into a section and then show hide the section instead directly show/hide the webresource control.
Fourth.
You should do null check when you are getting the optionset value , if there is no default value assigned in the optionset field.
Fifth-
I can see you mentioned you are using Dynamics CRM 2016 , so forcontext suggested above will not work in your case, formcontext concepts release in version D365 9.0.
Here is the final code which is working for me , do following -
1- Add a section and drag the webresource inside it.
2- Take the field name ,section name and the tab name and replace in below code.
3- Register function in the form onload and optionset field onchange.
function ShowHideGreenFolderWR() {
var dealType = Xrm.Page.getAttribute("opt_type").getValue(); //REplace optionset field name
if (dealType != null) {
if (dealType == "924550000") { // Replace the optionset value for which you want to display webresource
Xrm.Page.ui.tabs.get("TabName").sections.get("SectionName").setVisible(true); //Replace TabName , sectionName
} else {
Xrm.Page.ui.tabs.get("TabName").sections.get("SectionName").setVisible(false);//Replace TabName , sectionName
}
}
else { // For null value webresource will not display
Xrm.Page.ui.tabs.get("TabName").sections.get("SectionName").setVisible(false); //Replace TabName , sectionName
}
}