Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

JavaScript Error Cannot read properties of null (reading 'getOptions')

Posted on by 30

I currently have a list of options in a choice that is very long. User wants to consolidate the list, but removing values from the option set in the table isn't an option. I'm attempting to hide certain options, but am running into the error "Cannot read properties of null (reading 'getOptions')" when the form loads. The option set is all lowercase and is spelled correctly, but I replaced it with "my_optionset". 

Here is my code: 

function hide_options(executionContext) {
// get form context
var formContext = executionContext.getFormContext();

// Get options from lead source. 
var optionValues = formContext.getControl('my_optionset').getOptions();

// Remove options from optionValues if not "Bid Notification System", "Contract Renewal", "Direct Sale", or "Marketing Generated"
for (let i = 0; i < optionValues.size; i++) {
if (optionValues[i] == "Bid Notification System" || optionValues[i] == "Contract Renewal" || optionValues[i] == "Direct Sale" || optionValues[i] == "Marketing Generated" || optionValues[i] == null) {
continue;
}
else {
formContext.getControl('ena_leadsource').removeOption(optionValues[i]);
}
}
}

  • Verified answer
    akellcla93 Profile Picture
    akellcla93 30 on at
    RE: JavaScript Error Cannot read properties of null (reading 'getOptions')

    Fixed it with a complete rework:

    function hide_options(executionContext) {

     // get form context

     var formContext = executionContext.getFormContext();

     // Get options from lead source

     var option = formContext.getControl('my_optionset');

     var optionValues = option.getAttribute().getOptions();

     // Clear option and get ready to add other options back

     option.clearOptions();

     // Add correct Bid Notification System, Contract Renewal, Direct Sale, and Marketing Generated back to option set, remove all others

     optionValues.forEach(function (opt) {

       if(opt.value == "1111111111" || opt.value == "1111111111" || opt.value == "111111111" || opt.value == "1111111111") {

         option.addOption(opt);

       }

       else {

         option.removeOption(opt);

       }

     });

    }

  • akellcla93 Profile Picture
    akellcla93 30 on at
    JavaScript Error Cannot read properties of null (reading 'getOptions')

    Want to preface this with I am newer to CRM administration, and this is my first time attempting to deploy a web resource. I currently have a list of options in a choice that is very long. User wants to consolidate the list, but removing values from the option set in the table isn't an option. I'm attempting to hide certain options, but am running into the error "Cannot read properties of null (reading 'getOptions')" when the form loads. The option set is all lowercase and is spelled correctly, but I replaced it with "my_optionset". 

    Here is my code: 

    function hide_options(executionContext) {
    // get form context
    var formContext = executionContext.getFormContext();

    // Get options from lead source. 
    var optionValues = formContext.getControl('my_optionset').getOptions();

    // Remove options from optionValues if not "Bid Notification System", "Contract Renewal", "Direct Sale", or "Marketing Generated"
    for (let i = 0; i < optionValues.size; i++) {
    if (optionValues[i] == "Bid Notification System" || optionValues[i] == "Contract Renewal" || optionValues[i] == "Direct Sale" || optionValues[i] == "Marketing Generated" || optionValues[i] == null) {
    continue;
    }
    else {
    formContext.getControl('ena_leadsource').removeOption(optionValues[i]);
    }
    }
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans