locked
Showing option in option set via javascript RRS feed

  • Question

  • Hi,

    In javascript I have removed some options in an option set based on the value of another field. The values are removed successfully but if the value is changed then the options associated with it don't get put back into the option set field. Is there a reverse of the removeoption command that i can use to show values?

    Thanks

    Tuesday, January 29, 2013 5:02 PM

Answers

  • Below example helps you:

    function OnLoad() {
        var optionsetControl = Xrm.Page.ui.controls.get("brd_statusreason");
        var options = optionsetControl.getAttribute().getOptions();
        var type = Xrm.Page.getAttribute("brd_status").getValue();
        var reason = Xrm.Page.getAttribute("brd_statusreason").getValue();
            // 'Active' is selected
            if (type == 172100000) {
                optionsetControl.clearOptions(); 
                for (var i = 0; i < options.length - 1; i++) {
                    if (i == 0 || i == 1 || i == 2) {
                        optionsetControl.addOption(options[i]);
                    }
                }
            }
            // 'Resolved' is selected
            else if (type == 172100001) {
                optionsetControl.clearOptions();
                for (var i = 0; i < options.length - 1; i++) {
                    if (i == 3 || i == 4) {
                        optionsetControl.addOption(options[i]);
                    }

                }
            }
            // 'Cancelled' is selected
            else if (type == 172100002) {
                optionsetControl.clearOptions();
                optionsetControl.addOption(options[5]);
            }
    Xrm.Page.getAttribute("brd_statusreason").setValue(reason);
     }


    If the answer helped you, remember to mark it as answer.


    Tuesday, January 29, 2013 6:45 PM
    Moderator

All replies

  • Hello,

    you need to use addOption to add those value back to option set.

    refer: http://msdn.microsoft.com/en-us/library/51828fe3-f6ff-4f97-80ed-b06b3a354955#BKMK_addOption


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, January 29, 2013 5:17 PM
    Moderator
  • Hello,

    To do that you will have to store the whole collection of options somewhere. For example:

    window.allPicklistOptions = Xrm.Page.getAttribute('picklist schema name').getOptions();

    and then restore back options using addOption method. For example:

    Xrm.Page.getControl('picklist schema name').addOption(window.allPicklistOptions[0]);


    Freelance Developer for Dynamics CRM 4.0/2011

    Tuesday, January 29, 2013 5:20 PM
    Moderator
  • Below example helps you:

    function OnLoad() {
        var optionsetControl = Xrm.Page.ui.controls.get("brd_statusreason");
        var options = optionsetControl.getAttribute().getOptions();
        var type = Xrm.Page.getAttribute("brd_status").getValue();
        var reason = Xrm.Page.getAttribute("brd_statusreason").getValue();
            // 'Active' is selected
            if (type == 172100000) {
                optionsetControl.clearOptions(); 
                for (var i = 0; i < options.length - 1; i++) {
                    if (i == 0 || i == 1 || i == 2) {
                        optionsetControl.addOption(options[i]);
                    }
                }
            }
            // 'Resolved' is selected
            else if (type == 172100001) {
                optionsetControl.clearOptions();
                for (var i = 0; i < options.length - 1; i++) {
                    if (i == 3 || i == 4) {
                        optionsetControl.addOption(options[i]);
                    }

                }
            }
            // 'Cancelled' is selected
            else if (type == 172100002) {
                optionsetControl.clearOptions();
                optionsetControl.addOption(options[5]);
            }
    Xrm.Page.getAttribute("brd_statusreason").setValue(reason);
     }


    If the answer helped you, remember to mark it as answer.


    Tuesday, January 29, 2013 6:45 PM
    Moderator
  • I've tried to use addOption along with getting all the options from the option set, but while it does add options back in, all of them are labelled 'undefined'.
    Wednesday, January 30, 2013 9:43 AM
  • I've tried to use addOption along with getting all the options from the option set, but while it does add options back in, all of them are labelled 'undefined'.
    If you add options in a way which I mentioned, first all of the options would be saved in a var, and then would be used again in addOption.

    If the answer helped you, remember to mark it as answer.

    Wednesday, January 30, 2013 9:59 AM
    Moderator
  • Hi, 

     Kindly check wheather you are adding options like this

     add options to optionset as follows 

        controlObj.addOption(option,[index]);

    if you are not adding option name it ll return undefined only

    (mark as answer and vote as helpful)

    Thanks,

    SASANK K


    Wednesday, January 30, 2013 11:00 AM