Options added via addOption() appear in the list but cannot be selected

Today I was trying to solve a little mystery for a colleague of mine. She was trying to dynamically add options to an empty option list in CRM 2015. The following code was used to do so:

function OnLoad() {

    Xrm.Page.getControl("new_optionset").addOption({ text: "One", value: 1 });
    Xrm.Page.getControl("new_optionset").addOption({ text: "Two", value: 2 });
    Xrm.Page.getControl("new_optionset").addOption({ text: "Three", value: 3 });
    Xrm.Page.getControl("new_optionset").addOption({ text: "Four", value: 4 });

    Xrm.Page.getAttribute("new_optionset").addOnChange(function () {
        alert(Xrm.Page.getAttribute("new_optionset").getValue());
    });
}

This way they appeared in the option list but when you selected one of them the option text was not shown and the value also wasn't available on save.

After some looking around I discovered that you cannot just add any text/value combination but it has to be an existing option from CRM. So after adding 4 test options in CRM...

... I was able to first remove them with clearOptions and after that add 3 of them to prove it worked this way.

function OnLoad() {

    Xrm.Page.getControl("new_optionset").clearOptions();

    Xrm.Page.getControl("new_optionset").addOption({ text: "One", value: 100000000 });
    Xrm.Page.getControl("new_optionset").addOption({ text: "Two", value: 100000001 });
    Xrm.Page.getControl("new_optionset").addOption({ text: "Four", value: 100000003 });

    Xrm.Page.getAttribute("new_optionset").addOnChange(function () {
        alert(Xrm.Page.getAttribute("new_optionset").getValue());
    });
}

Now you can see the 3 options I added and you are able to select them as well

Comments (2) -

  • resume writing service

    1/5/2017 4:20:45 AM |

    It is nice to read your post and you give wonderful information. Please keep posting such kind of informative stuff. Interesting article share and this blog is impresses more people to reading that blog.

Loading