Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

After Setting Optionset the get options() is not returning the Optionset

(0) ShareShare
ReportReport
Posted on by 1,532

Hi,

On my formLoad

I am assigning new Optionsets base on pre Optionset setting.  I am clearing the original and adding new Optionsets

7506.FieldOptionSet.JPG

My below code seems to be working correctly.

function setSampleCodes() {
    var iselect = "?$top=5&$select=new_sampleId,new_name";  // Retreive over 250 records
    //var ifilter = "&$filter=(new_ChildQuestions/Id eq guid'" + questionid.replace(/{/g, "").replace(/}/g, "") + "')";
    var iorderby = "&$orderby=new_name";
    var options = iselect + iorderby;

    var res = SDK.REST.retrieveMultipleRecords("new_sample", options, sampleCodeCallBack, errorHandler, retrieveComplete);
}


function sampleCodeCallBack(retrievedData) {

    var sampleCodeControl = Xrm.Page.getControl("new_samplecode");
    var sampleCodeOptionSets = sampleCodeControl.getOptions();

    //Clear the option set on the control
    if (sampleCodeControl != null && sampleCodeControl != undefined)
        sampleCodeControl.clearOptions();
 
    //Loop and build a new option set for the control
    for (var sc = 0; sc < sampleCodeOptionSets.length; sc++) {
        var sampleOption = sampleCodeOptionSets[sc];
        if (retrievedData.length > 0) {
            for (var rd = 0; rd < retrievedData.length; rd++) {

                const sample = retrievedData[rd];

                var addopionsetValue = 100000000 + rd; // get all optionset value which will be added

                // add  all option  from the object retrievedData
                // Link a description to the option set
                if (sampleOption.value == addopionsetValue) {
                    sampleOption.text = sample.new_name;
                    sampleCodeControl.addOption(sampleOption);
                    Xrm.Page.getControl("new_samplecode").addOption(sampleOption);
                }
            }
        }
    }
}

7558.sample5Optionset.JPG

My problem is when using

Xrm.Page.getAttribute("new_samplecode").getOptions()

I am not getting the 4 Optionsets  I am getting the original Optionsets from the first image.

What am I doing wrong?

*This post is locked for comments

  • rthompson Profile Picture
    1,532 on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Thanks very much for you help Goutam,

    I got it working.  Here the final results.

    sampleGroupOptionset.JPG

    Here the final code

            function setSampleCodes() {
                var iselect = "?$top=500&$select=new_sampleId,new_name";  // Retreive over 250 records
    
                var iorderby = "&$orderby=new_name";
                var options = iselect + iorderby;
    
                SDK.REST.retrieveMultipleRecords("new_sample", options, sampleCodeCallBack, errorHandler, retrieveComplete);
            }
    
            function sampleCodeCallBack(retrievedData) {
    
                var slectedOptions = new Array();
                var arrayCnt = 0;
    
                var sampleCodeControl = Xrm.Page.getControl("new_samplecode");
                var sampleCodeOptionSets = sampleCodeControl.getOptions();
    
                //Clear the option set on the control
                if (sampleCodeControl != null && sampleCodeControl != undefined)
                    sampleCodeControl.clearOptions();
    
    
                if (retrievedData.length > 0) {
                    //Loop and build a new option set for the control
                    for (var sc = 0; sc < sampleCodeOptionSets.length; sc++) {
                        for (var rd = 0; rd < retrievedData.length; rd++) {
    
                            const sample = retrievedData[rd];
    
                            var addopionsetValue = 100000000 + rd; // get all optionset value which will be added
    
                            // add  all option  from the object retrievedData
                            // Link a description to the option set
                            var sampleOption = sampleCodeOptionSets[sc];
                            if (sampleOption.value === addopionsetValue && sampleOption.value > 0) {
                                sampleOption.text = sample.new_name;
                                //sampleCodeControl.addOption(sampleOption);
                                Xrm.Page.getControl("new_samplecode").addOption(sampleOption);
                                slectedOptions[arrayCnt] = new Object();
                                slectedOptions[arrayCnt].text = sampleOption.text;
                                slectedOptions[arrayCnt].name = sampleOption.value;
                                arrayCnt++;
                            }
                        }
                    }
                }
    
                ConvertDropDownToCheckBoxList(slectedOptions);
                //console.log('sampleCodeCallback called.');
                //Xrm.getControl.getAttribute.setValue(sampleCodeControl);
                //SampleCodeLoad(slectedOptions);
            }
    
    
            function retrieveComplete() {
                console.log('retrieveCompleted');
            }
    
            function errorHandler(error) {
                alert(error.message);
            }
    
            //Coverts option list to checkbox list.
            function ConvertDropDownToCheckBoxList(slectedOptions) {
                //var dropdownOptions = parent.Xrm.Page.getAttribute("new_samplecode").getOptions();
    
                var dropdownOptions = slectedOptions;
    
                var selectedValue = parent.Xrm.Page.getAttribute("new_name").getValue();
    
                $(dropdownOptions).each(function (i, e) {
                    var rText = $(this)[0].text;
                    var rvalue = $(this)[0].name;
                    var isChecked = false;
                    if (rText != '') {
                        $('<input />', { type: 'checkbox' })
                        .attr("value", rvalue)
                        .attr("checked", isChecked)
                        .attr("id", "id" + rvalue)
                        .click(function () {
                            //To Set Picklist Select Values
                            var selectedOption = parent.Xrm.Page.getAttribute("new_samplecode").getValue();
    
                            if (this.checked) {
                                if (selectedOption == null)
                                    selectedOption = rvalue + "";
                                else
                                    selectedOption = selectedOption + "," + rvalue
                            }
                            else {
                                var tempSelected = rvalue + ",";
                                if (selectedOption != null) {
                                    if (selectedOption.indexOf(tempSelected) != -1)
                                        selectedOption = selectedOption.replace(tempSelected, "");
                                    else
                                        selectedOption = selectedOption.replace(rvalue, "");
                                }
                            }
                            parent.Xrm.Page.getAttribute("new_samplecode").setValue(selectedOption);
    
    
                            //To Set Picklist Select Text
                            var selectedSampleCode = parent.Xrm.Page.getAttribute("new_name").getValue();
                            if (this.checked) {
                                if (selectedSampleCode == null)
                                    selectedSampleCode = rText + "";
                                else
                                    selectedSampleCode = selectedSampleCode + "," + rText
                            }
                            else {
                                var tempSelectedtext = rText + ",";
                                if (selectedSampleCode != null) {
                                    if (selectedSampleCode.indexOf(tempSelectedtext) != -1)
                                        selectedSampleCode = selectedSampleCode.replace(tempSelectedtext, "");
                                    else
                                        selectedSampleCode = selectedSampleCode.replace(rText, "");
                                }
                            }
                            parent.Xrm.Page.getAttribute("new_name").setValue(selectedSampleCode);
    
                        })
                        .appendTo(checkboxList);
                        $('<label>').text(rText).appendTo(checkboxList);
                        $('<br>').appendTo(checkboxList);
                    }
                });
            }
    


    Html

    <body style="word-wrap: break-word;">
        <div id="checkboxList">
        </div>
     
        <script type="text/javascript">
            (document.onreadystatechange = function () {
                if (document.readyState == "complete") {
                    try {
                        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                            httpRequest = new XMLHttpRequest();
                        } else if (window.ActiveXObject) { // IE
                            try {
                                httpRequest = new ActiveXObject("Msxml3.XMLHTTP");
                            }
                            catch (e) {
                                try {
                                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                                }
                                catch (e) { }
                            }
                        }
                    }
                    catch (e) {
                        // statements to handle any exceptions
                        alert(e); // pass exception object to error handler
                    }
     
                    if (!httpRequest) {
                        // alert('Giving up :( Cannot create an XMLHTTP instance');
                        return false;
                    }
                    setSampleCodes();
     
                }
     
            })();
     
        </script>
     
     
    </body>
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Hello,

    You need to create one html HTML web-resource and add that html page in the form.

    Please have a look below article -

    community.dynamics.com/.../convert-option-set-to-multi-select-checkbox-list

  • rthompson Profile Picture
    1,532 on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Thanks Goutam Das

    So how can I create a multiple picklist check box that I can use based on the array.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Sorry this is unsupported code , the reference you have shared is for CRM 4.0  and its might not work in future release of Dynamics 365. I believe you are not using Dynamics 365 multi select option set as you are not using Dynamics 365 V9.

    So going back to your previous  posted code you will get all the options in an array like below -

    function setSampleCodes() {
        var iselect = "?$top=5&$select=new_sampleId,new_name";  // Retreive over 250 records
        //var ifilter = "&$filter=(new_ChildQuestions/Id eq guid'" + questionid.replace(/{/g, "").replace(/}/g, "") + "')";
        var iorderby = "&$orderby=new_name";
        var options = iselect + iorderby;
    
        var res = SDK.REST.retrieveMultipleRecords("new_sample", options, sampleCodeCallBack, errorHandler, retrieveComplete);
    }
    
    
    function sampleCodeCallBack(retrievedData) {
        var slectedOptions = new Array();
        var sampleCodeControl = Xrm.Page.getControl("new_samplecode");
        var sampleCodeOptionSets = sampleCodeControl.getOptions();
    
        //Clear the option set on the control
        if (sampleCodeControl != null && sampleCodeControl != undefined)
            sampleCodeControl.clearOptions();
     
        //Loop and build a new option set for the control
        for (var sc = 0; sc < sampleCodeOptionSets.length; sc++) {
            var sampleOption = sampleCodeOptionSets[sc];
            if (retrievedData.length > 0) {
                for (var rd = 0; rd < retrievedData.length; rd++) {
    
                    const sample = retrievedData[rd];
    
                    var addopionsetValue = 100000000 + rd; // get all optionset value which will be added
    
                    // add  all option  from the object retrievedData
                    // Link a description to the option set
                    if (sampleOption.value == addopionsetValue) {
                        sampleOption.text = sample.new_name;
                        sampleCodeControl.addOption(sampleOption);
                        Xrm.Page.getControl("new_samplecode").addOption(sampleOption);
                        slectedOptions[0] = new Object();
                        slectedOptions[0].text = sampleOption.text;
                        slectedOptions[0].name = sampleOption.value; 
                    }
                }
            }
        }
    }


    so you can use slectedOptions array object instead of Xrm.page.getControl("new_samplecode").getOptions();.

  • rthompson Profile Picture
    1,532 on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Okay.  Here's where I am at.

    I found this link that I am sure you might be already aware of to create a multi picklist

    https://blogs.msdn.microsoft.com/crm/2009/03/31/crm-4-0-checkbox-style-multi-select-picklist/

    I am able to update the Optionset but can't get the picklist to display in multi check boxes.

    3264.sample5Optionset.JPG

    
    
    
    
    
    
    
    
    I am trying to use this code
    
    This "crmForm.all" does not seem to work.  When I try using "sampleCodeControl.getOptions();" and "Xrm.page.getControl("new_samplecode").getOptions();" this does not work either.
    
    And the PL.style.display does not seem to work at all.  Been working on this issue for days any ideal on what I am doing wrong.
    
    function SampleCodeLoad(sampleCodeControl) {
    
        alert("In function SampleCodeLoad");
    
        // PL - the picklist attribute; PLV - used to save selected picklist values  
        // var PL = Xrm.page.getControl("new_samplecode").getOptions();    //CREATE NEW PICKLIST
        //var PL = sampleCodeControl.getOptions();
        //var PLV = Xrm.Page.getAttribute("new_name").getValue();  //CREATE NEW TEXT FIELD TO STORE STRING
    
        // PL – the picklist attribute; PLV – used to save selected picklist values  
    
        var PL = crmForm.all.new_samplecode;
        var PLV = crmForm.all.new_name;
    
        if (PL != null && PLV != null) {
            PL.style.display = "none";
            PLV.style.display = "none";
    
            // Create a DIV container  
            var addDiv = document.createElement("<div style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />");
    
            PL.parentNode.appendChild(addDiv);
    
            // Initialise checkbox controls  
            for (var i = 0; i < PL.options.length; i++) {
                var pOption = PL.options[i];
    
                if (!IsChecked(pOption.text))
                    var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />");
                else
    
                var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />");
                var addLabel = document.createElement("<label />");
    
                addLabel.innerText = pOption.text;
    
                var addBr = document.createElement("<br>"); //it's a 'br' flag  
                PL.nextSibling.appendChild(addInput);
                PL.nextSibling.appendChild(addLabel);
                PL.nextSibling.appendChild(addBr);
            }
        }
    
        // Check if it is selected  
        function IsChecked(pText) {
            if (PLV.value != "") {
                var PLVT = PLV.value.split("||");
                for (var i = 0; i < PLVT.length; i++) {
                    if (PLVT[i] == pText)
                        return true;
                }
            }
            return false;
        }
    }


  • rthompson Profile Picture
    1,532 on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Okay.  Here's where I am at.

    I found the link that I am sure you might be already aware of to create a multi picklist

    https://blogs.msdn.microsoft.com/crm/2009/03/31/crm-4-0-checkbox-style-multi-select-picklist/

    I am able to update the Optionset.  But can't get the picklist to display.

    2313.sample5Optionset.JPG

    I am trying to use this code

    This "crmForm.all" does not seem to work.  When I try "sampleCodeControl.getOptions();" and "Xrm.page.getControl("new_samplecode").getOptions();"

    And the PL.style.display does not seem to work either.  Been working on this days and ideal on what I am doing wrong.

    function SampleCodeLoad(sampleCodeControl) {

        alert("In function SampleCodeLoad");
    
        // PL - the picklist attribute; PLV - used to save selected picklist values  
        // var PL = Xrm.page.getControl("new_samplecode").getOptions();    //CREATE NEW PICKLIST
        //var PL = sampleCodeControl.getOptions();
        //var PLV = Xrm.Page.getAttribute("new_name").getValue();  //CREATE NEW TEXT FIELD TO STORE STRING
    
        // PL – the picklist attribute; PLV – used to save selected picklist values  
    
        var PL = crmForm.all.new_samplecode;
        var PLV = crmForm.all.new_name;
    
        if (PL != null && PLV != null) {
            PL.style.display = "none";
            PLV.style.display = "none";
    
            // Create a DIV container  
            var addDiv = document.createElement("<div style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />");
    
            PL.parentNode.appendChild(addDiv);
    
            // Initialise checkbox controls  
            for (var i = 0; i < PL.options.length; i++) {
                var pOption = PL.options[i];
    
                if (!IsChecked(pOption.text))
                    var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />");
                else
    
                var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />");
                var addLabel = document.createElement("<label />");
    
                addLabel.innerText = pOption.text;
    
                var addBr = document.createElement("<br>"); //it's a 'br' flag  
                PL.nextSibling.appendChild(addInput);
                PL.nextSibling.appendChild(addLabel);
                PL.nextSibling.appendChild(addBr);
            }
        }
    
        // Check if it is selected  
        function IsChecked(pText) {
            if (PLV.value != "") {
                var PLVT = PLV.value.split("||");
                for (var i = 0; i < PLVT.length; i++) {
                    if (PLVT[i] == pText)
                        return true;
                }
            }
            return false;
        }
    }

  • gdas Profile Picture
    50,091 Moderator on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    Hello,

    This is expected - Xrm.Page.getAttribute("new_samplecode").getOptions() will return all options which your optionset  field contains .  As an alternate , I think as you are adding the options using loops so you can declare an array and all the option in the array , so you will get what are the options added in the optionset. 

    Hope this helps.

  • Suggested answer
    Friyank Profile Picture
    944 on at
    RE: After Setting Optionset the get options() is not returning the Optionset

    I guess this is not possible using javascript

    you cannot change the metadata.

    This will not allow someone else to query your new data.

    check this following links

    https://community.dynamics.com/crm/f/117/t/144155

    https://stackoverflow.com/questions/20658554/crm-2013-dynamic-optionset-issue

    https://social.microsoft.com/Forums/en-US/e2f23d69-80d2-4c50-a23f-030454dd8626/crm-2011-adding-option-to-optionset-using-javascript?forum=crmdevelopment

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans