Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

Posted on by 177

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

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    Great, please mark the helpful suggestions to help people in the community by closing threads

  • Suggested answer
    saturation Profile Picture
    saturation 177 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    The 2nd option works great (and I see the point of the first one as well)--thank you!

  • saturation Profile Picture
    saturation 177 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    Perfect! Exactly what I needed--thank you so much!

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    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);
    }
  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

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


  • saturation Profile Picture
    saturation 177 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    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?

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

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


  • Suggested answer
    saturation Profile Picture
    saturation 177 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

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

               }  

         }

    }

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

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


  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: Prevent Users from Selecting 2 Values in an Option Set (or remove them!)

    Hi saturation,

    I would suggest you to use first approach in your case. Use removeOption function  to remove options dynamically as suggested by Goutam

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans