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 :
Microsoft Dynamics CRM (Archived)

How to reset default optionset values after removing it?

(0) ShareShare
ReportReport
Posted on by

Hi,

I have a requirement to setup depending optionset. 

Optionset 1 has

1. Fruit

2. All

Optionset 2 has 10 values. If I click fruit, it removing 5 non-fruit values, it is ok. but when I click again All, it not reseting all default values, until I have to refresh the page.

My Script is 

function removeoptions() {
var options = Xrm.Page.getAttribute("new_optionset1").getValue(); 

var defauktvalues = Xrm.Page.getAttribute("new_optionset2").getValue(); 

if (options == 1) {
Xrm.Page.getControl("new_optionset2").removeOption(5);
Xrm.Page.getControl("new_optionset2").removeOption(6);
Xrm.Page.getControl("new_optionset2").removeOption(7);
Xrm.Page.getControl("new_optionset2").removeOption(8);
} else if (options == 2) {

Xrm.Page.getAttribute("new_optionset2").setValue(defaultvalues);

}

Appreciate your help. thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    You need to check condition like

    If(optionsetvale==All)

    Xrm.Page.getControl("new_optionset2").addOption(1);

    Xrm.Page.getControl("new_optionset2").addOption(2);

    Xrm.Page.getControl("new_optionset2").addOption(3);

    And so on

    Hope this helps

    Best regards,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    if (options == 1) {

    Xrm.Page.getControl("new_optionset2").removeOption(5);

    Xrm.Page.getControl("new_optionset2").removeOption(6);

    Xrm.Page.getControl("new_optionset2").removeOption(7);

    Xrm.Page.getControl("new_optionset2").removeOption(8);

    } else if (options == 2) {

    Add all the optionset value here again like

    Xrm.Page.getControl("new_optionset2").addOption(5);

    }

    Please mark my answer Verified if it helps you

    Best regards,

    Shahbaaz

  • ashlega Profile Picture
    34,477 on at

    And, also, looks like you have a typo in your code.. defauKtvalues up at the top vs defauLtvalues down at the bottom of the script

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    you can use my dependent optionset solution, check the demo and see if it can works for you.

    crmoptionsets.azurewebsites.net

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    I have rewrited  your code.Please try - hope this helps.

    function removeoptions() {

    var options = Xrm.Page.getAttribute("new_optionset1").getValue();

    var defaultvalues = Xrm.Page.getAttribute("new_optionset2").getValue();

    if (options == 1) {

    Xrm.Page.getControl("new_optionset2").removeOption(5);

    Xrm.Page.getControl("new_optionset2").removeOption(6);

    Xrm.Page.getControl("new_optionset2").removeOption(7);

    Xrm.Page.getControl("new_optionset2").removeOption(8);

    } else if (options == 2) {

    Xrm.Page.getControl("new_optionset2").addOption(5);

    Xrm.Page.getControl("new_optionset2").addOption(6);

    Xrm.Page.getControl("new_optionset2").addOption(7);

    Xrm.Page.getControl("new_optionset2").addOption(8);

    Xrm.Page.getAttribute("new_optionset2").setValue(defaultvalues);

    }

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello ,

    You can try another way - Here is the code .

    function removeoptions() {
        var BaseOptionsSetValue = Xrm.Page.getAttribute("new_optionset1").getValue();
    
        var optionSet = Xrm.Page.ui.controls.get("new_optionset2");
        var optionSetValues = optionSet.getAttribute().getOptions();
        if (optionSet != null && optionSet != undefined)
            optionSet.clearOptions();
        optionSetValues.forEach(function (element) {
            //Remove
            if (BaseOptionsSetValue == 1) {
                if (element.value > 4) // Add all element for Fruit  - (Value >4 will be removed)
                {
                    optionSet.removeOption(element);              
                }
    
            } else if (BaseOptionsSetValue == 2) {
                optionSet.addOption(element);           
            }
        });
    }
  • Community Member Profile Picture
    on at

    Your solution is good, if both optionset in a same section of form. It is working good. I have multiple sections on forms and somehow it is not working. Any idea?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Could you please try to debug the code share the error.

    As JavaScript running on page level ,so control should get no matter from which section you are calling as all the section are belongs to same page.

  • Community Member Profile Picture
    on at

    I solved it. It was not different section issue. Thanks everone to helpout.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Ok great!!! Please close the thread and verified if your problem has been fixed to help others in future.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans