web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Error Cannot read property 'text' of null of a OptionSet field

(0) ShareShare
ReportReport
Posted on by 160

Hi,

I have a OptionSet field on form and i wrote a code to hide a tab on this form if specific item is selected, But if no item is selected, I've got an Error: 

6404.pastedimage1629061026319v1.png

function showtab (executionContext){

formContext = executionContext.getFormContext();
var Title = formContext.getAttribute('cos_joblevel').getSelectedOption().text;
if (Title != null){
if (Title == "Apprentice")

{
formContext.ui.tabs.get("tab_evaluationformapprentice").setVisible(true);
}
else
{

formContext.ui.tabs.get("tab_evaluationformapprentice").setVisible(false);
}
}
}

Can anybody help me with this problem?

Thank you in advance!

I have the same question (0)
  • Verified answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi,

    You need to check if the selectedOption is not null. 

    if (formContext.getAttribute('cos_joblevel').getSelectedOption()!=null){

    //yourCode

    }

  • Verified answer
    Community Member Profile Picture
    on at

    Hi,

    If that item is not selected, `formContext.getAttribute('cos_joblevel').getSelectedOption()` will be null, and then calling `text` will cause the error you see, it is recommended to be like what `Mehdi El Amri` said First judge whether it is null.

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Frank Gong

  • Elpibato Profile Picture
    160 on at

    Hi, Thank you.

    It works now.

  • Suggested answer
    Victor carlos Profile Picture
    20 on at
    var customerValue = Xrm.Page.getAttribute("cos_joblevel").getValue();
    if (customerValue != null) {
    // ...
    } else {
    // ...
    }

    check this article:

    community.dynamics.com/.../don-39-t-use-getselectedoption-text

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
ManoVerse Profile Picture

ManoVerse 55 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans