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;
}