I'm trying to make a multi-select choice field values selected dynamically. I have tried many different way but nothing seems to work, is that possible to do? Description is below:
I have 2 multi-select fields Divisions and Departments. Departments is filtered based on the selected Divisions, which works. However, I need to re-select the previously selected Departments if they are in the selected Division, otherwise unselect.
Below is one of the many variations of the code that I have tried:
var departmentIdsToSelect = [];
field.widget.bind("dataBound", function() {
var availableDepartments = field.widget.dataSource.data();
for(y = 0; y < selectedDepartmentIds.length; y++){
for (i = 0; i < availableDepartments.length; i++) {
if (availableDepartments[i].LookupId == selectedDepartmentIds[y]) { //selectedDepartmentIds holds the ids of the selected departments before the filter is applied.
departmentIdsToSelect.push(selectedDepartmentIds[y]);
break;
}
}
}
fd.field('Departments').value = null;
fd.field('Departments').value = departmentIdsToSelect;
})
This does not work in that it will not remove the department when it is not even part of the available list. For example "Family and Community Services" should not be selected because it is not part of the available departments the drop down.
Thanks for the solution, it mostly worked but with a glitch, as shown in the screenshot the values are being duplicated randomly, not all the time. In that situation I had previously selected the Division "Senior Administration", but removed it and then selected it after all.