Skip to main content

Notifications

Dynamics 365 general forum
Answered

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

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!

  • Suggested answer
    Victor carlos Profile Picture
    Victor carlos 20 on at
    RE: Error Cannot read property 'text' of null of a OptionSet field
    var customerValue = Xrm.Page.getAttribute("cos_joblevel").getValue();
    if (customerValue != null) {
    // ...
    } else {
    // ...
    }

    check this article:

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

  • Elpibato Profile Picture
    Elpibato 160 on at
    RE: Error Cannot read property 'text' of null of a OptionSet field

    Hi, Thank you.

    It works now.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Error Cannot read property 'text' of null of a OptionSet field

    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

  • Verified answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: Error Cannot read property 'text' of null of a OptionSet field

    Hi,

    You need to check if the selectedOption is not null. 

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

    //yourCode

    }

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans