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 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,216 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

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 51 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans