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:
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);
}
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
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);
}
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.
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.
Yes, here is what I have:
I also tried adding "all" under parameter section at the bottom, but that gave me an error as well. Any ideas?
Thanks!
Hi!
Here's an example:
Did you select the Library (first dropdown) that includes the method FilterOppType?
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.
I think you might have registered the function/event in the incorrect library of the form.
Regards.
Use debugger and debug your script to know the issue.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156