web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

JScript to Show/Hide Web resource on form

(0) ShareShare
ReportReport
Posted on by 175

Hi, I have a requirement for a simple JScript function.  I think I can piece together some of it as a non-developer, but am looking for the code regardless

Requirement: on Load/Save of form, or when field "dealreviewtype" = 'Green Folder Review', show web resource "publisher_webresourcename".  Else, on Load/Save of form, or when field "dealreviewtype" = 'LOI Review' or ' Contract Review', hide web resource "publisher_webresourcename". 

Any help is appreciated!

Thanks again.

pastedimage1573588471302v1.png

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    hi William,

    as I don't know the exact field names, I can write you some example code. Only one thing, if the field "Deal Review Type" is an optionset, you need to check the value and not the label

    function HideShowWebResource() {

    var dealType = Xrm.Page.getAttribute("publisher_dealreviewtype").getValue();

    if (dealType == 1) {

    // 1 = Green Folder Review

    Xrm.Page.getControl("publisher_webresourcename").setVisible(true);

    } else {

    Xrm.Page.getControl("publisher_webresourcename").setVisible(false);

    }

    }

    note: it's just an example

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    The syntax for hiding the web resource is below: If you set "setVisible(false)" then it will show again.

    ---

    var control = Xrm.Page.ui.controls.get("WebResource_name");

    control.setVisible(false);

    ---

    The completed code will look like this:

    ---

    var control = Xrm.Page.ui.controls.get("WebResource_name");

    var desiredValue = Xrm.Page.getAttribute("new_dealreviewtype").getValue();

    //check if value is equal to value 'Green Folder Review'

    if(desiredValue == 100000000){

    control.setVisible(false);

    }

    ---

    For adding script on form events please refer - www.c5insight.com/.../how-to-use-javascript-on-dynamics-crm-201120132015-form.aspx

  • William Bradley Profile Picture
    175 on at

    Thanks so much for the help.  Does the second piece of code need to be added then under the first piece?  

    Also, I am getting a script error after implementing the first piece of code.  I updated the value of the Option Set to set "Green Folder = 2".  Here is what I have (**to maintain anonymity to the client, i removed the client name and replaced with 'publisher')

    function ShowHideGreenFolderWR()

    {

    var dealType = Xrm.Page.getAttribute(publisher_dealreviewtype).getValue();

    if (dealType == 2) {

    Xrm.Page.getControl("publisher_greenfoldereview").setVisible(true);

    } else {

    Xrm.Page.getControl("publisher_greenfolderreview").setVisible(false);

    }

    }

    And here is the error:

    ReferenceError: lennar_dealreviewtype is not defined

       at ShowHideGreenFolderWR (lennardev.crm.dynamics.com/.../lennar_VisibilityofGreenFolderWR:3:38)

       at cp.executeFunction (lennardev.crm.dynamics.com/.../app.js

       at cp.execute (lennardev.crm.dynamics.com/.../app.js

       at rp._executeIndividualEvent (lennardev.crm.dynamics.com/.../app.js

       at rp._executeEventHandler (lennardev.crm.dynamics.com/.../app.js

       at Object.execute (lennardev.crm.dynamics.com/.../app.js

       at S._executeSyncAction (lennardev.crm.dynamics.com/.../app.js

       at S._executeSync (lennardev.crm.dynamics.com/.../app.js

       at S.executeAction (lennardev.crm.dynamics.com/.../app.js

       at t.dispatch (lennardev.crm.dynamics.com/.../app.js

  • William Bradley Profile Picture
    175 on at

    I corrected some syntax in the above - now get this error:

    TypeError: Cannot read property 'setVisible' of null

       at ShowHideGreenFolderWR (lennardev.crm.dynamics.com/.../lennar_VisibilityofGreenFolderWR:13:48)

       at cp.executeFunction (lennardev.crm.dynamics.com/.../app.js

       at cp.execute (lennardev.crm.dynamics.com/.../app.js

       at rp._executeIndividualEvent (lennardev.crm.dynamics.com/.../app.js

       at rp._executeEventHandler (lennardev.crm.dynamics.com/.../app.js

       at Object.execute (lennardev.crm.dynamics.com/.../app.js

       at S._executeSyncAction (lennardev.crm.dynamics.com/.../app.js

       at S._executeSync (lennardev.crm.dynamics.com/.../app.js

       at S.executeAction (lennardev.crm.dynamics.com/.../app.js

       at t.dispatch (lennardev.crm.dynamics.com/.../app.js

  • William Bradley Profile Picture
    175 on at

    Here is where I am with the code now:

    function ShowHideGreenFolderWR()

    {

    var dealType = formContext.getControl("lennar_dealreviewtype").getValue();

    if (dealType == 2) {

    formContext.getControl("WebResource_GreenFolderReview").setVisible(true);

    } else {

    formContext.getControl("WebResource_GreenFolderRreview").setVisible(false);

    }

    The error log now looks like this:

    The log file now looks like this:

     

    ReferenceError: Web resource method does not exist: ShowHideGreenFolderWR

        at cp.execute (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1080:2576)

        at rp._executeIndividualEvent (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:18983)

        at rp._executeEventHandler (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:17978)

        at Object.execute (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:17612)

        at S._executeSyncAction (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:692)

        at S._executeSync (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:419)

        at S.executeAction (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:201)

        at t.dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:5545)

        at Object.dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1467:1359)

        at dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:5034)

    Any ideas?

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi There,

    If are trying to hide web resource using xrm library then try with ternary operator as mentioned below:

    Xrm.Page.getControl("WebResource_GreenFolderReview") ? Xrm.Page.getControl("WebResource_GreenFolderReview").setVisible(true) : false;

    and

    Xrm.Page.getControl("WebResource_GreenFolderReview") ? Xrm.Page.getControl("WebResource_GreenFolderReview").setVisible(false) : false;

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Hi William,

    function ShowHideGreenFolderWR(executionContext)

    {

    var formContext = executionContext.getFormContext();

    var dealType = formContext.getControl("lennar_dealreviewtype").getValue();

    if (dealType == 2) {

    formContext.getControl("WebResource_GreenFolderReview").setVisible(true);

    } else {

    formContext.getControl("WebResource_GreenFolderRreview").setVisible(false);

    }

    }

    and Why you use this function in onload or onSave Event Put it on Onchnage event of lennar_dealreviewtype Field . 

    How to do that.

    pastedimage1573711686317v2.png

    pastedimage1573711236028v1.png

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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
        }
    }

      

  • William Bradley Profile Picture
    175 on at

    Thank you so much for responding, Pravin Pawar.  When I replace my code with the above, I get the same error.

    function ShowHideGreenFolderWR(context) { var formContext = context.getFormContext();

    {

    var dealType = formContext.getControl("lennar_dealreviewtype").getValue();

    if (dealType == 2)

    {

    Xrm.Page.getControl("WebResource_GreenFolderReview") ? Xrm.Page.getControl("WebResource_GreenFolderReview").setVisible(true) : false;

    } else {

    Xrm.Page.getControl("WebResource_GreenFolderReview") ? Xrm.Page.getControl("WebResource_GreenFolderReview").setVisible(false) : false;

    }

    The log file now looks like this:

     

    ReferenceError: Web resource method does not exist: ShowHideGreenFolderWR

        at cp.execute (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1080:2576)

        at rp._executeIndividualEvent (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:18983)

        at rp._executeEventHandler (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:17978)

        at Object.execute (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1077:17612)

        at S._executeSyncAction (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:692)

        at S._executeSync (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:419)

        at S.executeAction (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:201)

        at t.dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:5545)

        at Object.dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:1467:1359)

        at dispatch (https://lennardev.crm.dynamics.com/uclient/scripts/app.js?v=1.3.2399-191022-211210:560:5034)

  • William Bradley Profile Picture
    175 on at

    Thank you Ajyendra Singh for responding.  I do have the event running OnChange of Deal Review Type field above, with Pass execution context checked.  Still getting the Web Resource Method does not exist error when the Option Set changes.  

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans