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

Announcements

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

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

#1
Subra Profile Picture

Subra 107

#2
11manish Profile Picture

11manish 91

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 85 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans