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 365 | Integration, Dataverse...
Suggested Answer

Having issues hiding/showing options in an option set based on the option selection in another option set.

(0) ShareShare
ReportReport
Posted on by 106

Objective :  I have 2 option sets(Gender and Hobbies).
So based on the Gender selection i want to show/hide options in my option set hobbies.

// JavaScript source code
function check() {

    var gender = Xrm.Page.getAttribute('hhh_gender').getSelectedOption().value;

    var RoleSubCategoryOptionSet = Xrm.Page.ui.controls.get('hhh_hobbies');
    RoleSubCategoryOptionSet.clearOptions();

    for (var i = 0; i < RoleSubCategoryOptionSet.length; i++) {
        // get control name stored in the array element.
        var att = RoleSubCategoryOptionSet[i];
        // set the control value to null to make it clear.
        Xrm.Page.getattribute(att).setvalue(null);
    }
    if (gender != null) {
        if (gender = 1) {
            RoleSubCategoryOptionSet.clearOptions();
            //Xrm.Page.getControl('hhh_hobbies').removeOption(2);
            //Xrm.Page.getControl('hhh_hobbies').removeOption(3);
            Xrm.Page.getControl('hhh_hobbies').addOption('Sports',1);
            Xrm.Page.getControl('hhh_hobbies').addOption('Dancing',4);
        }
        else if (gender = 2) {
            RoleSubCategoryOptionSet.clearOptions();
            //Xrm.Page.getControl('hhh_hobbies').removeOption(1);
            //Xrm.Page.getControl('hhh_hobbies').removeOption(4);
            Xrm.Page.getControl('hhh_hobbies').addOption('Cooking',2);
            Xrm.Page.getControl('hhh_hobbies').addOption('Singing',3);
        }
        else {
            alert("Select option sir.");
        }
    }
}
Getting error :
Required parameter is null or undefined: optionSetItem.value.

I have the same question (0)
  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Kashyap,

    You should pass object into addOption function if you would like to add an option to optionset.

    Xrm.Page.getControl("hhh_hobbies").addOption({value: 1, text: 'Sports'});

    API Reference:

    https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg334266(v=crm.8)?redirectedfrom=MSDN#addoption

    Demo:

    https://itsfascinating.com/d365/tag/addoption/

    pastedimage1579595308792v1.png

    Regards,

    Clofly

  • Suggested answer
    ba365guy Profile Picture
    2,950 on at

    .addOption(option, index)

    In the above method, "option" is an object with two parameters:

    text: String. The label for the option.
    value: Number. The value for the option.

    Index is separate optional parameter.

    I think you got error because you did not use the method correctly.

  • Mayank Pujara Profile Picture
    830 on at
  • cloflyMao Profile Picture
    25,210 on at

    Hi Kashyap,

    Could it solve your problem?

    Regards,

    Clofly

    Please kindly mark as verified if you had found any answer helped, it would be really appreciated.

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans