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 :
Customer experience | Sales, Customer Insights,...
Answered

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

(0) ShareShare
ReportReport
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]);
}
}
}

I have the same question (0)
  • akellcla93 Profile Picture
    30 on at

    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]);
    }
    }
    }

  • Verified answer
    akellcla93 Profile Picture
    30 on at

    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);

       }

     });

    }

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans