Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to reset default optionset values after removing it?

Posted on by Microsoft Employee

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

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to reset default optionset values after removing it?

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to reset default optionset values after removing it?

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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to reset default optionset values after removing it?

    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
    Community Member Microsoft Employee on at
    RE: How to reset default optionset values after removing it?

    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
    gdas 50,085 on at
    RE: How to reset default optionset values after removing it?

    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);           
            }
        });
    }
  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to reset default optionset values after removing it?

    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
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: How to reset default optionset values after removing it?

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

    crmoptionsets.azurewebsites.net

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to reset default optionset values after removing it?

    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
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How to reset default optionset values after removing it?

    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

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: How to reset default optionset values after removing it?

    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

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans