I am using a JavaScript button to set a Field-2 on each selected list item from "false" to "true". It first checks if Field-1 is empty, and if it is, sets Field-2 to true. The relevant code is as follows:
function ApproveAllSelected() {
this.context = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
var list = context.get_web().get_lists().getByTitle("List Title Here");
this.items = [];
var item;
for (item in selectedItems) {
var approvalItem = list.getItemById(selectedItems[item].id);
items.push(approvalItem);
context.load(approvalItem, "Field1", "Field2");
}
context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), OnFailed);
window.location = document.location;
window.location.href = window.location;
}
function OnSuccess(sender, args) {
for (var j = 0; j < this.items.length; j++) {
var value = this.items[j].get_item("Field2");
if (value == null)
{
this.items[j].set_item("Field1", 1);
this.items[j].update();
this.context.executeQueryAsync();
}
}
}
The strange thing is, this code is successful, but seems to only be successful on 4-5 list items at once. If I select 20 list items and then hit the button, only 4-5 have Field-2 set successfully, and the other list items act as if nothing happened. Other times, nothing in the list changes. I am refreshing the list between tries as well.
There are no errors thrown from either On-failed or in my SharePoint ULS logs. I see no JavaScript errors on the page in my browser debugger, either.
If I continuously re-select the items and press the approval button over and over, the items will eventually all be marked as "true".