Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Need help filtering an option set using JavaScript

Posted on by Microsoft Employee

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

}

  • Suggested answer
    JayBuddhdevtest Profile Picture
    JayBuddhdevtest 275 on at
    RE: Need help filtering an option set using JavaScript

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Need help filtering an option set using JavaScript

    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
    Aditya Profile Picture
    Aditya 260 on at
    RE: Need help filtering an option set using JavaScript

    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/

  • Suggested answer
    RE: Need help filtering an option set using JavaScript

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Need help filtering an option set using JavaScript

    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!

  • RE: Need help filtering an option set using JavaScript

    Hi! 

    Here's an example: 

    pastedimage1593122753663v1.png

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Need help filtering an option set using JavaScript

    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.

  • Suggested answer
    RE: Need help filtering an option set using JavaScript

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

    Regards.

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Need help filtering an option set using JavaScript

    Use debugger and debug your script to know the issue.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans