2
votes

I have a CRM 2013 form with an empty optionset and I need to use Javascript to dynamically add options when the form is loaded.

In the onLoad method of the form, I'm making a jQuery ajax call to get the list of items to add to the optionset, then I'm looping through the results and calling the addOption method on the control to add the items.

The new options are showing up in the optionset just fine, but when I run the form and select one of the dynamic options, the selection doesn't "stick", and the selected option immediately becomes NULL. It doesn't even trigger the onChange event of the optionset control.

Can anyone help me with this? I feel like it must me possible because I've seen dozens of blog posts for dynamically adding options and I am following exactly, but it doesn't work.

function form_Load()
{
    if (!formLoaded) {
        try {
            $.ajax({
                url: TECHNOLOGIES_URL,
                dataType: 'jsonp',
                success: function (response) {
                    $('#results').html(JSON.stringify(response, null, '    '));
                    var children = response.children;
                    var arrayLength = children.length;

                    // update the optionset
                    var xrmPage = Xrm.Page;
                    var pickListField = xrmPage.getControl("et_parenttechnology");
                    for (var i = 0; i < arrayLength; i++) {
                        //add items to parent technology option set
                        var option = { value : children[i].id , text : children[i].name };
                        pickListField.addOption(option);

                    }
                }
            });
        }
        catch (ex) { debugLog("test: " + ex.message); }
    }
    formLoaded = true;
}
2
Exactly my issue too. - Amogh Natu

2 Answers

0
votes

It is not possible to add values with JS. You will have to add them before, hide them and then show only the ones you want. Finally, you will have to replace your "Add" operation with a "Show" one.

0
votes

You could technically use the SDK to add and remove option set values. You could take the code in this example (http://msdn.microsoft.com/en-us/library/gg509023.aspx) and run it in the SOAPLogger project in the SDK to get the SOAP request which could in turn be executed via JavaScript.

However this is changing the system metadata of the field and isn't something that is recommended that you do via the client. By using the method in the answer of creating all possible values and then hiding them you preserve the data in other records. If you were to actually delete the piece of metadata associated with a given option, it would delete the value from all records in the system.