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,...
Suggested Answer

Need help filtering an option set using JavaScript

(0) ShareShare
ReportReport
Posted on by

Hello,

I’ve created a Javascript web resource to filter an option set using this article as a guide: https://neilparkhurst.com/2015/12/01/javascript-control-optionset-values/

 

I want to be able to filter the Opportunity Type (option set) field based on what is selected in the Owning Team (Text) field.  However, I keep receiving the following error when I try to run it:

FilterOppType.PNG

 

Below is my code.  How can I fix this error so the code successfully filters the Opportunity Type?

 

var analytics = {value : 701, text : "Analytics"};

var auto = {value : 100000000, text : "Automation / IAC"};

var burwood = {value : 801, text : "Burwood Cloud & Managed Svcs"};

var healthcare = {value : 401, text : "Healthcare Advisory Services"};

 

 

function FilterOppType(all) {

  var pickList = Xrm.Page.getControl("new_opportunitytype");

  var options = Xrm.Page.getAttribute("new_opportunitytype").getOptions();

  var owningteam = Xrm.Page.getAttribute("new_owningteamtext");

 

  // *** Clear current items

  for (var i = 0; i < options.length; i++) {

    pickList.removeOption(options[i].value);

  }

 

  // *** Now add back just what is needed

  if (owningteam == "Healthcare") {

    pickList.addOption(healthcare);

  }

 

  pickList.addOption(analytics);

  pickList.addOption(auto);

  pickList.addOption(burwood);

}

I have the same question (0)
  • Dynamics365 Rocker Profile Picture
    7,755 on at

    Use debugger and debug your script to know the issue.

  • Suggested answer
    Alejandro Serdan Profile Picture
    on at

    I think you might have registered the function/event in the incorrect library of the form.

    Regards.

  • Community Member Profile Picture
    on at

    Hi Alex, thanks for your reply. Can you explain in greater detail what you mean? I've added the function to my opportunity form and set it as an OnChange event for the Owning Team field.

  • Alejandro Serdan Profile Picture
    on at

    Hi! 

    Here's an example: 

    pastedimage1593122753663v1.png

    Did you select the Library (first dropdown) that includes the method FilterOppType? 

  • Community Member Profile Picture
    on at

    Yes, here is what I have:

    3582.Properties2.PNG

    I also tried adding "all" under parameter section at the bottom, but that gave me an error as well.  Any ideas?

    Thanks!

  • Suggested answer
    Alejandro Serdan Profile Picture
    on at

    First, if possible, use the new method as Xrm.Page is deprecated:

    Some client APIs are deprecated

    docs.microsoft.com/.../important-changes-coming

    Then, to receive "all" parameter, will be equivalent to the check "Pass execution context as first parameter" as per code. This will be required to get the form context.

    getFormContext (Client API reference)

    docs.microsoft.com/.../getFormContext

    Finally:

    addOption (Client API reference)

    docs.microsoft.com/.../addoption

    Syntax

    formContext.getControl(arg).addOption(option, index);

    Regards.

  • Suggested answer
    Aditya Profile Picture
    260 on at

    Hi Tgiard,

    As mentioned Xrm.Page is deprecated, so you need to use formcontext in your script and pass the executioncontext while registering the event.

    https://carldesouza.com/using-formcontext-in-dynamics-365/

  • Community Member Profile Picture
    on at

    Thank you both! I've updated my code based on your feedback (See below). I've also checked the "Pass execution context as first parameter" box. However, I'm still getting the same error message "Web resource method does not exist". Any ideas how I can fix this?

    Below is my code. Hopefully I didn't misunderstand any of your recommendations:

    var analytics = {value : 701, text : "Analytics"};

    var auto = {value : 100000000, text : "Automation / IAC"};

    var burwood = {value : 801, text : "Burwood Cloud & Managed Svcs"};

    var healthcare = {value : 401, text : "Healthcare Advisory Services"};

    function FilterOppType(executionContext) {

    var formContext = executionContext.getFormContext();

     var pickList = formContext.getControl("new_opportunitytype");

     var options = formContext.getAttribute("new_opportunitytype").getOptions();

     var owningteam = formContext.getAttribute("new_owningteamtext");

     // *** Clear current items

     for (var i = 0; i < options.length; i++) {

       pickList.removeOption(options[i].value);

     }

     // *** Now add back just what is needed

     if (owningteam == "Healthcare") {

    formContext.getControl(executionContext).addOption(healthcare, 1);

     }

    formContext.getControl(executionContext).addOption(analytics, 1);

    formContext.getControl(executionContext).addOption(auto, 2);

    formContext.getControl(executionContext).addOption(burwood, 3);

    }

  • Suggested answer
    JayBuddhdevtest Profile Picture
    275 on at

    Hi,

    You shouldn't use executionContext in the get Control(arg). You should use it like this:

    formContext.getControl("new_opportunitytype").addOption(healthcare, 1);

    Change this for all 4 syntax that you have and it should work. Also add console.log(), and inspect it to see where the problem is.

    I hope this helps. Please let me know if you have any questions/concerns.

    Thanks

    Jay

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 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans