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)

Cannot read property '0' of null

(0) ShareShare
ReportReport
Posted on by

Hello, 

I am trying to hide a tab on the form if the segmantid lookup is equal to "Hospitals". When the lookup value is null i get the following error : Cannot read property '0' of null at hideTab

function hideTab() {
Xrm.Page.ui.tabs.get("tab_2").setVisible(false);
var segment = Xrm.Page.getAttribute("segmentid").getValue();
if (segment[0].name == "Hospitals") {
Xrm.Page.ui.tabs.get("tab_2").setVisible(true);
}
else if (!segment || segment.length == 0){
Xrm.Page.ui.tabs.get("tab_2").setVisible(false);
}
else {
Xrm.Page.ui.tabs.get("tab_2").setVisible(false);
}
}

How can i fix this?

Thanks

*This post is locked for comments

I have the same question (0)
  • Verified answer
    JohnAnonymous Profile Picture
    5,241 on at

    I would think it's this line: segment[0].name

    You need to check if segment contains any objects before you pick the first one of the list.

    Tip: place a debugger on the second line and you can check for yourself with debugging in the browser.

  • Verified answer
    Radu Chiribelea Profile Picture
    6,667 on at

    Hi Nexius,

    When the segmentid is null then following line of code var segment = Xrm.Page.getAttribute("segmentid").getValue(); will return a null, hence causing if (segment[0].name == "Hospitals") to throw the error. Because we are trying to access an item of a null array.

    I suggest you add an extra validation before. Something like

    if (segment != null && segment[0].name == "Hospitals")

    Hope this helps

    Radu

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try following code:

    function hideTab() {

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

    var segment = Xrm.Page.getAttribute("segmentid").getValue();

    if (segment == null || segment.length == 0){

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

    }

    else if (segment[0].name == "Hospitals") {

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

    }

    else {

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

    }

    }

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