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 :
Microsoft Dynamics CRM (Archived)

JavaScript OnLoad , OnChange

(0) ShareShare
ReportReport
Posted on by 274

This question is similar to this one, I am just updating the question with the field names, also I am not using a business rule because I used the maximum number 

If "Is today a holiday?"  Equals Yes
   Show "Describe the holiday"
   Required "Describe the holiday"
Else
   Hide "Describe the Holiday"
   Not Required "Describe the Holiday"

The field "Is today a holiday" is an option set with Yes and No. The default value is blank

The field "Describe the holiday" is a text field

function onLoad() {
    Test("new_istodayaholiday");
}

function new_istodayaholidayOnChange() {
    Test("new_istodayaholiday");
}

function Test(fieldName) {
    var checked = Xrm.Page.getAttribute(fieldName).getValue();

    var required = checked ? "required" : "none";

    Xrm.Page.getControl("new_describetheholiday").setVisible(checked);
    Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel(required);
}

I tried the above code as suggested on the previous post, but I am getting this error:

TypeError: Cannot read property 'setVisible' of null

I really appreciate the help.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

      if (Xrm.Page.getControl("new_describetheholiday") = null) {

             setTimeout(Test(fieldName), 2000);

      }

  • Verified answer
    Community Member Profile Picture
    on at

    Hi,

    Try below code :

    function Test(fieldName) {

       if(Xrm.Page.getAttribute(fieldName).getValue()) {

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

       Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel("required");

    }

    else

     {

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

       Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel("none");

      }

    }

  • MituCRMing Profile Picture
    274 on at

    Hi Mohit,

    The above code doesn't work it need to happen onLoad and onChange

    that's why the code I have in the post uses onLoad and onChange

    Thanks

  • Verified answer
    Community Member Profile Picture
    on at

    Hi,

    you need to replace test function, i have done the changes in that function, use below code and thn try :

    function onLoad() {

       Test("new_istodayaholiday");

    }

    function new_istodayaholidayOnChange() {

       Test("new_istodayaholiday");

    }

    function Test(fieldName) {

      if(Xrm.Page.getAttribute(fieldName).getValue()) {

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

      Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel("required");

    }

    else

    {

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

      Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel("none");

     }

    }

  • MituCRMing Profile Picture
    274 on at

    Hi Mohit,

    I am getting this error onLoad: TypeError: Cannot read property 'setVisible' of null

    Thanks

  • Suggested answer
    Community Member Profile Picture
    on at

    i think you have not put this field on form "new_describetheholiday" thats why you are getting this error. Please add this "new_describetheholiday" field on the form and then try

  • MituCRMing Profile Picture
    274 on at

    Hi Mohit,

    I do have the field in the form, that's where I added the form property. I still get the same error

    TypeError: Cannot read property 'setVisible' of null at Test

    Thanks

  • Suggested answer
    Community Member Profile Picture
    on at

    Can you check whether your field name is correct "new_describetheholiday"

  • MituCRMing Profile Picture
    274 on at

    Hi Mohit,

    There was a typo, I passed that error but I am getting this Hi Mohit,

    ReferenceError: required is not defined at Test

    Thanks

  • Suggested answer
    Community Member Profile Picture
    on at

    Pass required in quotes like "required" not required

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans