I have an option set with about 6 values (customer, prospect, lead, competitor, etc). I need to prevent users from selecting 2 of the values in the option set as they are set through a business process flow behind the scenes (specifically, customer and prospect). How can either
1) Dynamically remove these from the optionset list values when the form loads, or
2) Prevent the users from changing the values to either of those?
*This post is locked for comments
Great, please mark the helpful suggestions to help people in the community by closing threads
The 2nd option works great (and I see the point of the first one as well)--thank you!
Perfect! Exactly what I needed--thank you so much!
Hi,
You want to keep 3 & 8 or you want to remove them.
if you want to keep 3 & 8 then add if (optionSetValues[i].value != '3' || optionSetValues[i].value != '8'). so that except Customer and Prospect remaining all will be cleared from the list.
or if you have more options to remove and very few to add(remain in list) then do below,
function onLoad(executionContext) { Xrm.Page.getControl("customertypecode").clearOptions(); Xrm.Page.getControl("customertypecode").addOption(3);
Xrm.Page.getControl("customertypecode").addOption(8);
}
you can use this if you want to consider each value separate
function onLoad(executionContext) { var formContext = executionContext.getFormContext(); var customer = formContext.getAttribute("customertypecode").getValue(); if (customer !=3) { formContext.getControl("customertypecode").removeOption(3); } if (customer !=8) { formContext.getControl("customertypecode").removeOption(8); } }
or this if you must consider them acting together
function onLoad(executionContext) { var formContext = executionContext.getFormContext(); var customer = formContext.getAttribute("customertypecode").getValue(); if (customer !=3 && customer !=8) { formContext.getControl("customertypecode").removeOption(3); formContext.getControl("customertypecode").removeOption(8); } }
This works as well. I now need to figure out how to NOT remove them if the selected value is 3 or 8 (or "customer" or "prospect"). Do I need to check for
formContext.getControl("customertypecode").value or is there some other way to check for selected value?
I am sorry if I add another response, but getting the options through the attribute property and foreach the items is too unnecessary for your requirement. As you already using the new formContext syntax, the code can be
function onLoad(executionContext) { var formContext = executionContext.getFormContext(); formContext.getControl("customertypecode").removeOption(3); formContext.getControl("customertypecode").removeOption(8); }
Had to tweak your code just a bit but used this and called in the onLoad event:
function onLoad(executionContext) {
var formContext = executionContext.getFormContext();
var optionSet = formContext.getControl("customertypecode");
var optionSetValues = optionSet.getAttribute().getOptions();
for (i = 0; i < optionSetValues.length; i++) {
try {
if (optionSetValues[i].value == '3' || optionSetValues[i].value == '8') //option is not customer or prospect
{
formContext.getControl("customertypecode").removeOption(optionSetValues[i].value);
}
}
catch (ex) {
console.log(ex.message);
}
}
}
you just need two lines of code, assuming your field name is "new_field" and the values you want to remove are 1 and 2
Xrm.Page.getControl("new_field").removeOption(1); Xrm.Page.getControl("new_field").removeOption(2);
Hi saturation,
I would suggest you to use first approach in your case. Use removeOption function to remove options dynamically as suggested by Goutam
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,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156