0

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.");
}

1 Answer 1

1

JSOM stores the records in asynchronous manner. You can give a try to below JSOM code to store records in bulk.

function savetoDetailList(id) {
    var ctx = new SP.ClientContext.get_current();
    var oList = ctx.get_web().get_lists().getByTitle("DetailList");
   
    var items = [];
    var batchLimit = 100;
    
    for (var i = 0; i < 5000; i++) {
        var item = globalAuditTrailArr[i];
        
        var itemCreateInfo = new SP.ListItemCreationInformation();
        var oListItem = oList.addItem(itemCreateInfo);
        
        oListItem.set_item("Title", item.Title);
        oListItem.set_item("FIELD_x0020_DESCRIPTION", item.fieldDesc);
        oListItem.set_item("MainID", id); //MainList/Parent ListID

        oListItem.update();     
        items[i] = oListItem;       
        ctx.load(items[i]);

        if (i % batchLimit == 0) {                               
            ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            items = [];
        }            
    }
}

function onQuerySucceeded(sender, args) {
  console.log('Item created successfully.');
}

function onQueryFailed(sender, args) {
   console.log('Request failed.' + args.get_message() + '\n' + args.get_stackTrace());
}

You can use Rest API if you want to store the records in a synchronized way.

1
  • Thanks let me check but how we can be sure that all records are all saved?
    – BizApps
    Jan 24 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.