locked
CRM 2011 JavaScript assistance needed. RRS feed

  • Question

  • I have been working on the following javascript for awhile now and keep getting error that function is undefined. I can not see where my code is incorrect. Any assistance would be greatly appreciated. I am trying to show/hide a field and set the requirement leve based on the value in an option set.

    function onClientReportingReportTypeChange() {
        var reportType = Xrm.Page.getAttribute("new_reporttype").getText();
    	if (reportType == "Standard") {
            Xrm.Page.getAttribute("nhs_reportname").setRequiredLevel("none")
    	Xrm.Page.getAttribute("nhs_reportname").setVisible(false)
            } 
    	else if (reportType == "Custom") {
            Xrm.Page.getAttribute("nhs_reportname").setRequiredLevel("required")
    	Xrm.Page.getAttribute("nhs_reportname").setVisible(true)
            }
    }

    Wednesday, April 16, 2014 8:24 PM

Answers

  • Usually that means either the function name is spelled wrong (which it seems it isn't), or you're missing a curly brace in your library somewhere which is causing the whole file to error.

    If your library is not too big, paste it here and we can check for you, otherwise if you open it in Visual Studio it should tell you if there are any syntax errors.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    • Marked as answer by JenniferNHS Wednesday, April 16, 2014 10:03 PM
    Wednesday, April 16, 2014 9:57 PM

All replies

  • Sounds like your event handler might not be using the correct name. Check your onload/onchange event (depending on when the error occurs) and make sure that the function in the event handler is 'onClientReportingReportTypeChange'.

    The code looks fine though.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Wednesday, April 16, 2014 8:52 PM
  • setVisible is related to controls, not attributes. You need to use getControl

    you are missing also some semicolons

    function onClientReportingReportTypeChange() {
        var reportType = Xrm.Page.getAttribute("new_reporttype").getText();
    	if (reportType == "Standard") {
            Xrm.Page.getAttribute("nhs_reportname").setRequiredLevel("none");
    	Xrm.Page.getControl("nhs_reportname").setVisible(false);
            } 
    	else if (reportType == "Custom") {
            Xrm.Page.getAttribute("nhs_reportname").setRequiredLevel("required");
    	Xrm.Page.getControl("nhs_reportname").setVisible(true);
            }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile


    Wednesday, April 16, 2014 9:38 PM
  • Unfortunately that was not it.

    The exact message is:

    There was an error with this field's customized event.

    Field: new_reporttype

    Event:onChange

    Error:'onClientReportingReportTypeChange' is undefined

    Wednesday, April 16, 2014 9:42 PM
  • I changed my code to the suggest getControl however I get the same error.
    Wednesday, April 16, 2014 9:43 PM
  • Probably you have multiple libraries in your form and you picked up another one (not the one containing your function)


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, April 16, 2014 9:49 PM
  • Good call. I do have mulitple libraries however the correct one is selected.
    Wednesday, April 16, 2014 9:51 PM
  • Usually that means either the function name is spelled wrong (which it seems it isn't), or you're missing a curly brace in your library somewhere which is causing the whole file to error.

    If your library is not too big, paste it here and we can check for you, otherwise if you open it in Visual Studio it should tell you if there are any syntax errors.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    • Marked as answer by JenniferNHS Wednesday, April 16, 2014 10:03 PM
    Wednesday, April 16, 2014 9:57 PM
  • UCK!!! I swear I looked at that all afternoon and did not see the missing bracket. Thank you very much.
    Wednesday, April 16, 2014 10:03 PM