Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : axA+7T9BaCUo3Zo0t0ugSA
Microsoft Dynamics CRM (Archived)

JavaScript OnLoad , OnChange

Like (0) ShareShare
ReportReport
Posted on 14 Jun 2016 03:13:41 by 270

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 

  1. If "Is today a holiday?" Equals Yes
  2. Show "Describe the holiday"
  3. Required "Describe the holiday"
  4. Else
  5. Hide "Describe the Holiday"
  6. 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

  1. function onLoad() {
  2. Test("new_istodayaholiday");
  3. }
  4.  
  5. function new_istodayaholidayOnChange() {
  6. Test("new_istodayaholiday");
  7. }
  8.  
  9. function Test(fieldName) {
  10. var checked = Xrm.Page.getAttribute(fieldName).getValue();
  11.  
  12. var required = checked ? "required" : "none";
  13.  
  14. Xrm.Page.getControl("new_describetheholiday").setVisible(checked);
  15. Xrm.Page.getAttribute("new_describetheholiday").setRequiredLevel(required);
  16. }

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

  • MituCRMing Profile Picture
    MituCRMing 270 on 19 Jun 2016 at 23:32:39
    RE: JavaScript OnLoad , OnChange

    Thank you guys, I would like to show/hide some fields from the different tabs I have.  I modified the code as shown in the bold below for Tab 2, What I am trying to achieve is if fieldname01 equals yes the comment box will be displayed in Tab 2. 

    function onLoad() {

       showHide("new_type", "new_checklist");

       test("new_fieldname01", "new_fieldname02", "new_fieldname03");

    }

    function new_typeOnChange() {

       showHide("new_type");

    }

    function new_typeOnChange() {

       test("new_fieldname01", "new_fieldname02", "new_fieldname03");

    }

    function showHide() {

       var selectedType = Xrm.Page.getAttribute("new_type").getValue();

       var checklistValue = Xrm.Page.getAttribute("new_checklist").getValue();

       if (selectedType == 7 || checklistValue) {

           Xrm.Page.ui.tabs.get("General").sections.get("General").setVisible(true);

           Xrm.Page.ui.tabs.get("Tab2").setVisible(true);

           Xrm.Page.ui.tabs.get("Tab3").setVisible(true);

       }

       else {

           Xrm.Page.ui.tabs.get("General").sections.get("General").setVisible(false);

           Xrm.Page.ui.tabs.get("Tab2").setVisible(false);

           Xrm.Page.ui.tabs.get("Tab3").setVisible(false);

       }

    }

    function test(fieldName) {

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

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

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

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

       }

       else {

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

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

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

       }

    }

  • Verified answer
    Community Member Profile Picture
    Community Member on 17 Jun 2016 at 03:59:47
    RE: JavaScript OnLoad , OnChange

    Hi,

    Confirm whether you have passed correct tab name or not, yes you have to place review function in else condition

  • MituCRMing Profile Picture
    MituCRMing 270 on 17 Jun 2016 at 03:51:45
    RE: JavaScript OnLoad , OnChange

    Okay,thanks that solves the onLoad error however, Tab2 or Tab 3 are not expanding?

    Also I would like to execute a function called "review" after the if before the else. Can  I just use then to call the function?

    Thanks

  • Verified answer
    BharatPremji Profile Picture
    BharatPremji 2,485 on 17 Jun 2016 at 03:36:33
    RE: JavaScript OnLoad , OnChange

    Hi,

    That error suggests a syntax error in your code.

    It looks like your missing the curly braces, try this:

    function onLoad() {
    showHide("new_type", "new_checklist");
    }
    function new_typeOnChange() {
    showHide("new_type");
    }
    function showHide() {
    var selectedType = Xrm.Page.getAttribute("new_type").getValue();
    var selectedType = Xrm.Page.getAttribute("new_checklist").getValue();
    if (selectedType == 7 && checklistValue) {
    Xrm.Page.ui.tabs.get("General").sections.get("General").setVisible(true);
    Xrm.Page.ui.tabs.get("Tab2").setVisible(true);
    Xrm.Page.ui.tabs.get("Tab3").setVisible(true);
    }
    else {
    Xrm.Page.ui.tabs.get("General").sections.get("General").setVisible(false);
    Xrm.Page.ui.tabs.get("Tab2").setVisible(false);
    Xrm.Page.ui.tabs.get("Tab3").setVisible(false);
    }
    }

    I also noticed you have a logic error:

    var selectedType = Xrm.Page.getAttribute("new_type").getValue();
    var selectedType = Xrm.Page.getAttribute("new_checklist").getValue();

    Should that second line be:

    var checklistValue= Xrm.Page.getAttribute("new_checklist").getValue();

  • MituCRMing Profile Picture
    MituCRMing 270 on 17 Jun 2016 at 03:14:48
    RE: JavaScript OnLoad , OnChange

    Hi Mohit,

    I added the other tabs that I need to show/hide but it failed onLoad

    The error message is "ReferenceError: onLoad is not defined" Thanks!

    function onLoad() {

       showHide("new_type", "new_checklist");

    }

    function new_typeOnChange() {

       showHide("new_type");

    }

    function showHide() {

       var selectedType = Xrm.Page.getAttribute("new_type").getValue();

       var selectedType = Xrm.Page.getAttribute("new_checklist").getValue();

       if(selectedType == 7 && checklistValue )

       Xrm.Page.ui.tabs.get("General" ).sections.get("General").setVisible(true);

       Xrm.Page.ui.tabs.get("Tab2").setVisible(true);

       Xrm.Page.ui.tabs.get("Tab3").setVisible(true);

       else {

    Xrm.Page.ui.tabs.get("General").sections.get("General").setVisible(false);

        Xrm.Page.ui.tabs.get("Tab2").setVisible(false);

        Xrm.Page.ui.tabs.get("Tab3").setVisible(false);

        }

    }

  • MituCRMing Profile Picture
    MituCRMing 270 on 16 Jun 2016 at 16:29:38
    RE: JavaScript OnLoad , OnChange

    Okay, thanks. I still need to execute the function called "Review" before else. How can I achieve that?

  • Verified answer
    Community Member Profile Picture
    Community Member on 16 Jun 2016 at 15:22:56
    RE: JavaScript OnLoad , OnChange

    Hi,

    Please find the below code :

    I am assuming checklist attribute name is new_checklist, if the name is different than replace it with actual name

    function onLoad() {

      showHide("new_type", "new_checklist");

    }

    function new_typeOnChange() {

      showHide("new_type","new_checklist");

    }

    function  showHide(type, checklist) {

      var selectedType = Xrm.Page.getAttribute(type).getValue();

    var checklistValue = Xrm.Page.getAttribute(checklist).getValue();

      if (selectedType == 7 && checklistValue) {

          Xrm.Page.ui.tabs.get("General").sections.get("section 3").setVisible(true);

          Xrm.Page.ui.tabs.get("Tab 2").sections.get("section 1").setVisible(true);

      }

      else {

          Xrm.Page.ui.tabs.get("General").sections.get("section 3").setVisible(false);

          Xrm.Page.ui.tabs.get("Tab 2").sections.get("section 1").setVisible(false);

      }

    }

  • MituCRMing Profile Picture
    MituCRMing 270 on 16 Jun 2016 at 15:10:45
    RE: JavaScript OnLoad , OnChange

    Thank you Mohit!

    I am working in a little bit different scenario hid/show tabs and section. I have a field called Type, which is an option set and also I have another field called checklist which is a Y/N.  If Type ==7 or Checklist == Yes, I need to show General Tab and Section 3. The remaining Tabs and sections will be hidden. Also I need to execute a function called Review.

    Here is my code, I am not quite to sure where to put the "or" . Thanks

    function onLoad() {

       showHide("new_type");

    }

    function new_typeOnChange() {

       showHide("new_type");

    }

    function  showHide() {

       Xrm.Page.getAttribute("new_type")

       var selectedType = (Xrm.Page.getAttribute("new_type").getValue())

       if (selectedType == 7) {

           Xrm.Page.ui.tabs.get("General").sections.get("section 3").setVisible(true);

           Xrm.Page.ui.tabs.get("Tab 2").sections.get("section 1").setVisible(true);

       }

       else {

           Xrm.Page.ui.tabs.get("General").sections.get("section 3").setVisible(false);

           Xrm.Page.ui.tabs.get("Tab 2").sections.get("section 1").setVisible(false);

       }

    }

  • Verified answer
    Community Member Profile Picture
    Community Member on 15 Jun 2016 at 10:30:43
    RE: JavaScript OnLoad , OnChange

    As per the code, everything is fine, could you please confirm below :

    Data type of the field

    click ta button once you click on the No option value and then check.

    If still issue persist then you can debug your java-script and find the actual cause.

    you can do this requirement through OOB Business rules as well

  • MituCRMing Profile Picture
    MituCRMing 270 on 15 Jun 2016 at 10:22:53
    RE: JavaScript OnLoad , OnChange

    It is when I pick the value no it shows the description field and it's required as well.  When no is picked it doesn't need to be displayed

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,626 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,558 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete