I have weird issue saving multiple records to SharePoint List via JSOM using below code where sometimes most of the records were saved and sometimes were not.
Is there any best practice to save multiple records to SharePoint List via JSOM so it won't miss anything?
//globalAuditTrailArr contains 10 to 500 records depending on each transaction
function savetoDetailList(id) {
ctr = 0;
//iterate to array and save data to SharePoint List
globalAuditTrailArr.forEach((a) => {
var today = new Date();
var qyear = today.getFullYear();
var qmonth = today.getMonth();
var ctx = new SP.ClientContext.get_current();
var oList = ctx.get_web().get_lists().getByTitle("DetailList");
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item("Title", a.Title);
oListItem.set_item("FIELD_x0020_DESCRIPTION", a.fieldDesc);
oListItem.set_item("MainID", id); //MainList/Parent ListID
oListItem.update();
ctx.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
});
}
function onQuerySucceeded(sender, args) {
//Redirect to home page after saving
window.location.replace(siteurl);
}
function onQueryFailed(sender, args) {
console.log("Request failed.");
}