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,...
Suggested Answer

Can't read properties of null in javaScript in MS CRM

(0) ShareShare
ReportReport
Posted on by 5

function ShowHideTabs(executionContext) {

//Get the form contex
var formContext = executionContext.getFormContext();
var optionSetAttribute = formContext.getAttribute("mri_optionset").getValue();

if (formContext.getAttribute("mri_optionset").getValue() == 171800000) {
formContext.ui.tabs.get("tab_2").setVisible(true);

}
else (formContext.getAttribute("mri_optionset").getValue() != 0){
formContext.ui.tabs.get("tab_2").setVisible(false);
}
}

I have the same question (0)
  • bank Profile Picture
    5 on at

    Please let me know why I'm getting this Error.

    Thanks

  • bank Profile Picture
    5 on at

    function ShowHideTabs(executionContext) {

       //Get the form contex

       var formContext = executionContext.getFormContext();

       var optionSetAttribute = formContext.getAttribute("mri_optionset").getValue();

       if (optionSetAttribute!=null && optionSetAttribute == 171800000) {

           formContext.ui.tabs.get("tab_2").setVisible(true);

       }

       else {

           formContext.ui.tabs.get("tab_2").setVisible(false);

       }

    }

    I have tried this also

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi bank,

    Try to use the below code. The code cannot get that control, so you are getting the error.

    function showHideTab2(executionContext) {
        try {
            //Get the form contex
            var formContext = executionContext.getFormContext();
            var optionSetAttribute = getValue(formContext, "mri_optionset");
    
            if (optionSetAttribute == 171800000) {
                showHideTab(formContext, "tab_2", false);
    
            }
            else (optionSetAttribute != 0){
                showHideTab(formContext, "tab_2", false);
            }
        } catch (e) {
            openAlertDialog("Error from showHideTab2: "   "e: "   e   ". e.mesage: "   e.message);
        }
    }
    
    function showHideTab(formContext, tabName, isVisible) {
        try {
            let tabObj = formContext.ui.tabs.get(tabName);
            if (tabObj) {
                tabObj.setVisible(isVisible);
            }
        }
        catch (e) {
            openAlertDialog("Error from showHideTab: "   "e: "   e   ". e.mesage: "   e.message);
        }
    }
    
    function getValue(formContext, attribute) {
        let value = "";
        try {
            if (this.getControl(formContext, attribute) != "") {
                value = formContext.getAttribute(attribute).getValue();
            }
        } catch (e) {
            openAlertDialog("Error from getValue: "   "e: "   e   ". e.mesage: "   e.message);
        }
        return value;
    }
    
    function getControl(formContext, control) {
        let controlValue = ""
        try {
            controlValue = formContext.getControl(control);
        } catch (e) {
            openAlertDialog("Error from getControl: "   "e: "   e   ". e.mesage: "   e.message);
        }
        return controlValue
    }
    
    function openAlertDialog(alertStrings) {
        try {
            Xrm.Navigation.openAlertDialog(alertStrings).then();
        } catch (e) {
            Xrm.Navigation.openAlertDialog("Error from openAlertDialog: "   "e: "   e   ". e.mesage: "   e.message).then();
        }
    }

  • bank Profile Picture
    5 on at

    function ShowHideTabs(executionContext) {

           //Get the form contex

           var formContext = executionContext.getFormContext();

           var optionSetAttribute = getValue(formContext, "mri_optionset");

           if (optionSetAttribute == 171800000) {

               showHideTab(formContext, "tab_2", true);

           }

           else (optionSetAttribute!= 0)

               showHideTab(formContext, "tab_2", false);

           }

    getting the error: getValue is not defined

  • Suggested answer
    NODAL Profile Picture
    860 on at

    Hi,

    Check if attribute (mri_optionset) and tab (tab_2) names are correct.

    Use browser developer tool to identify exact issue.

    Refer this for more information : ://mscrmwithramandeep.home.blog/how-to-debug-javascript-in-dynamics-365-ce/

    Cheers,

    Ketan

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi bank,

    The below code should be worked as expected

    function showHideTab2(executionContext) {
        try {
            //Get the form contex
            var formContext = executionContext.getFormContext();
            var optionSetAttribute = getValue(formContext, "mri_optionset");
    
            if (optionSetAttribute == 171800000) {
                showHideTab(formContext, "tab_2", true);
    
            }
            else (optionSetAttribute !== 0){
                showHideTab(formContext, "tab_2", false);
            }
        } catch (e) {
            openAlertDialog("Error from showHideTab2: "   "e: "   e   ". e.mesage: "   e.message);
        }
    }
    
    function showHideTab(formContext, tabName, isVisible) {
        try {
            let tabObj = formContext.ui.tabs.get(tabName);
            if (tabObj) {
                tabObj.setVisible(isVisible);
            }
        }
        catch (e) {
            openAlertDialog("Error from showHideTab: "   "e: "   e   ". e.mesage: "   e.message);
        }
    }
    
    function getValue(formContext, attribute) {
        let value = "";
        try {
            if (this.getControl(formContext, attribute) !== "") {
                value = formContext.getAttribute(attribute).getValue();
            }
        } catch (e) {
            openAlertDialog("Error from getValue: "   "e: "   e   ". e.mesage: "   e.message);
        }
        return value;
    }
    
    function getControl(formContext, control) {
        let controlValue = ""
        try {
            controlValue = formContext.getControl(control);
        } catch (e) {
            openAlertDialog("Error from getControl: "   "e: "   e   ". e.mesage: "   e.message);
        }
        return controlValue
    }
    
    function openAlertDialog(alertStrings) {
        try {
            Xrm.Navigation.openAlertDialog(alertStrings).then();
        } catch (e) {
            Xrm.Navigation.openAlertDialog("Error from openAlertDialog: "   "e: "   e   ". e.mesage: "   e.message).then();
        }
    }

    Are you still not able to solve the problem?

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
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 36 Super User 2025 Season 2

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans