0

I am trying save the below into list in SharePoint, but each item is getting saved twice. I need to save only once. What's wrong?

function sumcalval() {

    alert("save function called");

    var pageload1 = 0;

    var rowCount1 = $("#data_table_1 tbody tr").length;
    alert("rowCount1 "+rowCount1);
    var rowcountgrid1 = rowCount1;
    alert("rowcountgrid1 " + rowcountgrid1);

    for (var j = 0; j < rowCount1; j++) {


        gridval1 = $("#row" + j).find(".projectcode").val();
        alert("gridval1 " + gridval1);
        gridval2 = $("#row" + j).find(".projectname").val();
        alert("gridval2" + gridval2);

        gridval3 = $("#row" + j).find(".atlaspartno").val();
        alert("gridval3" + gridval3);
        gridval4 = $("#row" + j).find(".atlasdescription").val();
        alert("gridval4" + gridval4);
        gridval5 = $("#row" + j).find(".stdnstd").val();
        alert("gridval5" + gridval5);
        gridval6 = $("#row" + j).find(".mfgpur").val();
        alert("gridval6" + gridval6);
        gridval7 = $("#row" + j).find(".finalqty").val();

        gridval8 = $("#row" + j).find(".uom").val();

        gridval9 = $("#row" + j).find(".qtyatquotequotedcogs").val();
        alert("gridval9" + gridval9);
        gridval10 = $("#row" + j).find(".unitratequotedcogs").val();

        gridval11 = $("#row" + j).find(".totalquotedcogs").val();

        gridval12 = $("#row" + j).find(".targetunitcost").val();

        gridval13 = $("#row" + j).find(".targetcosttotal").val();

        gridval14 = $("#row" + j).find(".rswp").val();


        gridval17 = $("#row" + j).find(".unitratepocosttracking").val();

        gridval18 = $("#row" + j).find(".pocosttotaltracking").val();

        gridval19 = $("#row" + j).find(".rswptracking").val();

        gridval20 = $("#row" + j).find(".unitraterevenue").val();

        gridval21 = $("#row" + j).find(".totalrevenue").val();

        //  TotalRevenue = TotalRevenue + gridvalue21;

        var ctx = new SP.ClientContext(appweburl);//Get the SharePoint Context object based upon the URL  
        var appCtxSite = new SP.AppContextSite(ctx, hostweburl);
        var web = appCtxSite.get_web(); //Get the Site  



        var list = web.get_lists().getByTitle("wbslist"); //Get the List based upon the Title  
        var listCreationInformation = new SP.ListItemCreationInformation(); //Object for creating Item in the List  
        var oListItem11 = list.addItem(listCreationInformation);

        oListItem11.set_item('wbscode', QueryStringValue);
        oListItem11.set_item('projectcode', listItemInfo1);
        oListItem11.set_item('projectname', listItemInfo2);

        oListItem11.set_item('WBSTaskno', gridvalue1);
        //oListItem1.set_item('TaskCategory', gridvaluecat);

        oListItem11.set_item('TaskName', gridval2);
        oListItem11.set_item('atlaspartno', gridval3);
        oListItem11.set_item('atlasdescription', gridval4);
        oListItem11.set_item('stdnstd', gridval5);
        oListItem11.set_item('mfgpur', gridval6);
        oListItem11.set_item('finalqty', gridval7);
        oListItem11.set_item('uom', gridval8);
        oListItem11.set_item('qtyatquotequotedcogs', gridval9);
        oListItem11.set_item('unitratequotedcogs', gridval10);
        oListItem11.set_item('totalquotedcogs', gridval11);
        oListItem11.set_item('targetunitcost', gridval12);
        oListItem11.set_item('targetcosttotal', gridval13);
        oListItem11.set_item('rswp', gridval14);
        //oListItem1.set_item('ponoitemwisefilledbypro', gridvalue15);
        oListItem11.set_item('unitratepocosttracking', gridval17);
        oListItem11.set_item('pocosttotaltracking', gridval18);
        oListItem11.set_item('rswptracking', gridval19);
        oListItem11.set_item('unitraterevenue', gridval20);
        oListItem11.set_item('totalrevenue', gridval21);
        oListItem11.update(); //Update the List Item  

        ctx.load(oListItem11);

        ctx.executeQueryAsync(function () {
            pageload++;

            if (pageload == rowcountgrid) {

                alert("Saving Completed");

            }
        }, 1);

    }



}

2 Answers 2

0

Please write below mention code outside the For-loop & try it.

var ctx = new SP.ClientContext(appweburl);
var appCtxSite = new SP.AppContextSite(ctx, hostweburl);
var web = appCtxSite.get_web(); //Get the Site  

var list = web.get_lists().getByTitle("wbslist");
var listCreationInformation = new SP.ListItemCreationInformation();  
var oListItem11 = list.addItem(listCreationInformation);
1
  • Items are not saving...
    – sherin
    Commented Jul 3, 2015 at 7:23
0

Try referencing with this so your listItem inside the methode is always the same:

function createListItem() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    oListItem.set_item('Title', 'My New Item!');
    oListItem.set_item('Body', 'Hello World!');

    oListItem.update();

    clientContext.load(oListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

https://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx

Your Answer

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

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