0

i am unable to add a list item. I need to update a field if the item already exsits in a list i was able to compare the lists to see if the item already exists. if the list does not exist then i need to add/copy the list item to the

I have figured out how to update if the item exists but i am unable to add if the item does not find a match.

please see the code below for more details.

<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var olist111;
    function AddToCart() {
        var context = SP.ClientContext.get_current();

        var web = context.get_web();
        context.load(web);

        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var oList = context.get_web().get_lists().getByTitle('Chart');
        context.load(oList);

        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        var spItems = [];

        //alert('debut');
        for (i = 0; i < selectedItems.length; i++) {
            var currentItem = source.getItemById(selectedItems[i].id);
            //alert(selectedItems[i].id);
            context.load(currentItem);
            spItems.push(currentItem);

        }


        context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {

            for (var j = 0; j < spItems.length; j++) {
                // pass use ID + product name
                var compareFun = Compare(spItems[j].get_item('Product_x0020_Name') + "#" + spItems[j].get_item('ID'));
                olist111 = spItems;
                compareFun.done(function (data) {
                    var Match = data;
                    var tempstr = Match.split("#");
                    var compared = tempstr[2];

                    if (compared == '1') {
                        //alert("Item already exists in the chart");

                        var tmpstr = Match.split("#");

                        var id = tmpstr[0];
                        var qty = tmpstr[1];
                        var clientContext = new SP.ClientContext();
                        var oList1 = clientContext.get_web().get_lists().getByTitle('Chart');
                        var oListItem = oList1.getItemById(id);

                        oListItem.set_item('QTY', parseInt(qty) + 1);
                        oListItem.update();
                        clientContext.executeQueryAsync(success, failed);
                    }

                    else if (compared == '0') {
                        alert("Item not in the chart");
                        var tmpstr = Match.split("#");

                        var Productname = tmpstr[0];
                        var id = tmpstr[1];
                        alert(id);
                        var clientContext = new SP.ClientContext.get_current();

                        var oList1 = clientContext.get_web().get_lists().getByTitle('Hardware Catalog');
                        var oListItem1 = oList1.getItemById(id);
                        clientContext.load(oListItem1);
                        clientContext.executeQueryAsync(success, failed);
                       // clientContext.load(oListItem1);
                        alert(111);
                       // alert(oListItem1.get_item('Product_x0020_Name'));


                        var oList = clientContext.get_web().get_lists().getByTitle('Chart');
                        //var chartListitem = oList.getItemById(id);
                        clientContext.load(oList);
                        var itemCreateInfo = new SP.ListItemCreationInformation();
                        var oListItem = oList.addItem(itemCreateInfo);
                        oListItem.set_item('Title', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Item', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Product_x0020_Name', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Description', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Price', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Department', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.set_item('Branch', oListItem1.get_item('Product_x0020_Name'));
                        oListItem.update();
                        clientContext.executeQueryAsync(success, failed);

                    }
                }).fail(function (sender, args) { alert('Failed'); });
                console.log("index" + spItems[j].get_item('Product_x0020_Name'));
            }
            function Succeeded() {

                alert('Item updated!');
            }
            //context.executeQueryAsync(success);
            //   alert('done');

        }), Function.createDelegate(this, error));
        function onQuerySucceeded(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function onQueryFailed(sender, args) {
            alert('Failed to add!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function failed(sender, args) {
            alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function Succeeded() {

            alert('Item updated!');
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function error(sender, args) { alert('error'); }
    }

    //function updateListItem(str) {
    //    alert(1);
    //    alert(str);
    //    var tmpstr = str.split("#");
    //    alert(tmpstr[0]);
    //    alert(tmpstr[1]);
    //    var Productname = tmpstr[0];
    //    alert('update');
    //    var clientContext = new SP.ClientContext();
    //    var oList = clientContext.get_web().get_lists().getByTitle('Chart');
    //    this.oListItem = oList.getItemById(tmpstr[1]);
    //    oListItem.set_item('QTY', 2);
    //    oListItem.update();
    //    alert('update 2');
    //    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    //}

    //function onQuerySucceeded() {

    //    alert('Item updated!');
    //}

    //function onQueryFailed(sender, args) {

    //    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    //}



    function Compare(str) {
        //alert(str);
        var Addstr = str;
        var dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        var id = tmpstr[1];
        var clientContext = SP.ClientContext.get_current();

        var list = clientContext.get_web().get_lists().getByTitle('Chart');

        var skillcamlQuery = new SP.CamlQuery();

        //skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        skillcamlQuery.set_viewXml("<View><Query> <Where><And><Eq> <FieldRef Name='Product_x0020_Name'/><Value Type='Text'>" + Productname + "</Value></Eq><Eq><FieldRef Name='Author' /><Value Type='Lookup'>" + _spPageContextInfo.userDisplayName + "</Value> </Eq> </And> </Where></Query></View>");

        var skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
            if (skillcollListItem.get_count() >= 1) {
                var listItemInfo = '';
                var listItemEnumerator = skillcollListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    str = oListItem.get_id() + "#" + oListItem.get_item('QTY');
                }
                dfd.resolve(str + "#" + 1);
            }
            else {
                //str = str + "#" + 0;
                dfd.resolve(Addstr + "#" + 0);
            }
        }), Function.createDelegate(this, function (sender, args) {
            dfd.reject(sender, args);
        }));
        return dfd.promise();
    }
</script>

 <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
0

The issue seems caused by your caml query, below code works based on my test, developer tool would helpful for front-end script always.

<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var olist111;
    function AddToCart() {
        var context = SP.ClientContext.get_current();

        var web = context.get_web();
        context.load(web);

        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var oList = context.get_web().get_lists().getByTitle('Chart');
        context.load(oList);

        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        var spItems = [];

        //alert('debut');
        for (i = 0; i < selectedItems.length; i++) {
            var currentItem = source.getItemById(selectedItems[i].id);
            //alert(selectedItems[i].id);
            context.load(currentItem);
            spItems.push(currentItem);

        }


        context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {

            for (var j = 0; j < spItems.length; j++) {
                // pass use ID + product name
                var compareFun = Compare(spItems[j].get_item('Product_x0020_Name') + "#" + spItems[j].get_item('ID'));
                olist111 = spItems;
                compareFun.done(function (data) {
                    var Match = data;
                    var tempstr = Match.split("#");
                    var compared = tempstr[2];

                    if (compared == '1') {
                        //alert("Item already exists in the chart");

                        var tmpstr = Match.split("#");

                        var id = tmpstr[0];
                        var qty = tmpstr[1];
                        var clientContext = new SP.ClientContext();
                        var oList1 = clientContext.get_web().get_lists().getByTitle('Chart');

                        var oListItem = oList1.getItemById(id);

                        oListItem.set_item('QTY', parseInt(qty) + 1);
                        oListItem.update();
                        clientContext.executeQueryAsync(success,failed);                 
                    }

                    else if (compared == '0') {
                        alert("Item not in the chart");
                        var tmpstr = Match.split("#");

                        var Productname = tmpstr[0];
                        var id = tmpstr[1];



                    }
                }).fail(function (sender, args) { alert('Failed'); });
                console.log("index" + spItems[j].get_item('Product_x0020_Name'));
            }
            function Succeeded() {

                alert('Item updated!');
            }
            //context.executeQueryAsync(success);
            //   alert('done');

        }), Function.createDelegate(this, error));
        function onQuerySucceeded(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function onQueryFailed(sender, args) {
            alert('Failed to add!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function failed(sender, args) {
            alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function Succeeded() {

            alert('Item updated!');
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function error(sender, args) { alert('error'); }
    }

    //function updateListItem(str) {
    //    alert(1);
    //    alert(str);
    //    var tmpstr = str.split("#");
    //    alert(tmpstr[0]);
    //    alert(tmpstr[1]);
    //    var Productname = tmpstr[0];
    //    alert('update');
    //    var clientContext = new SP.ClientContext();
    //    var oList = clientContext.get_web().get_lists().getByTitle('Chart');
    //    this.oListItem = oList.getItemById(tmpstr[1]);
    //    oListItem.set_item('QTY', 2);
    //    oListItem.update();
    //    alert('update 2');
    //    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    //}

    //function onQuerySucceeded() {

    //    alert('Item updated!');
    //}

    //function onQueryFailed(sender, args) {

    //    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    //}



    function Compare(str) {
        //alert(str);
        var Addstr = str;
        var dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        var id = tmpstr[1];
        var clientContext = SP.ClientContext.get_current();

        var list = clientContext.get_web().get_lists().getByTitle('Chart');

        var skillcamlQuery = new SP.CamlQuery();

        //skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Product_x0020_Name'/><Value Type='Text'>" + Productname + "</Value></Eq><Eq><FieldRef Name='Author' LookupId='TRUE'/><Value Type='Lookup'>" + _spPageContextInfo.userId+ "</Value></Eq></And></Where></Query></View>");

        var skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
            if (skillcollListItem.get_count() >= 1) {
                var listItemInfo = '';
                var listItemEnumerator = skillcollListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    str = oListItem.get_id() + "#" + oListItem.get_item('QTY');
                }
                dfd.resolve(str +"#"+ 1);
            }
            else {
                //str = str + "#" + 0;
                dfd.resolve(Addstr + "#" + 0);
            }
        }), Function.createDelegate(this, function (sender, args) {
            dfd.reject(sender, args);
        }));
        return dfd.promise();
    }
</script>

 <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 

Update:

Test code to add a item.

<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var olist111;
    function AddToCart() {
        var context = SP.ClientContext.get_current();

        var web = context.get_web();
        context.load(web);

        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var oList = context.get_web().get_lists().getByTitle('Chart');
        context.load(oList);

        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        var spItems = [];

        //alert('debut');
        for (i = 0; i < selectedItems.length; i++) {
            var currentItem = source.getItemById(selectedItems[i].id);
            //alert(selectedItems[i].id);
            context.load(currentItem);
            spItems.push(currentItem);

        }


        context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {

            for (var j = 0; j < spItems.length; j++) {
                // pass use ID + product name
                var compareFun = Compare(spItems[j].get_item('Product_x0020_Name') + "#" + spItems[j].get_item('ID'));
                olist111 = spItems;
                compareFun.done(function (data) {
                    var Match = data;
                    var tempstr = Match.split("#");
                    var compared = tempstr[2];

                    if (compared == '1') {
                        //alert("Item already exists in the chart");

                        var tmpstr = Match.split("#");

                        var id = tmpstr[0];
                        var qty = tmpstr[1];
                        var clientContext = new SP.ClientContext();
                        var oList1 = clientContext.get_web().get_lists().getByTitle('Chart');

                        var oListItem = oList1.getItemById(id);

                        oListItem.set_item('QTY', parseInt(qty) + 1);
                        oListItem.update();
                        clientContext.executeQueryAsync(success,failed);                 
                    }

                    else if (compared == '0') {
                        alert("Item not in the chart");
                        var tmpstr = Match.split("#");

                        var Productname = tmpstr[0];
                        var id = tmpstr[1];

                        var clientContext = new SP.ClientContext.get_current();
                        var oList = clientContext.get_web().get_lists().getByTitle('Chart');
                        //var chartListitem = oList.getItemById(id);
                        clientContext.load(oList);
                        var itemCreateInfo = new SP.ListItemCreationInformation();
                        var oListItem = oList.addItem(itemCreateInfo);
                        oListItem.set_item('Product_x0020_Name', "Some Value");
                        oListItem.update();
                        clientContext.executeQueryAsync(success, failed);

                    }
                }).fail(function (sender, args) { alert('Failed'); });
                console.log("index" + spItems[j].get_item('Product_x0020_Name'));
            }
            function Succeeded() {

                alert('Item updated!');
            }
            //context.executeQueryAsync(success);
            //   alert('done');

        }), Function.createDelegate(this, error));
        function onQuerySucceeded(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function onQueryFailed(sender, args) {
            alert('Failed to add!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function failed(sender, args) {
            alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function Succeeded() {

            alert('Item updated!');
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function error(sender, args) { alert('error'); }
    }

    //function updateListItem(str) {
    //    alert(1);
    //    alert(str);
    //    var tmpstr = str.split("#");
    //    alert(tmpstr[0]);
    //    alert(tmpstr[1]);
    //    var Productname = tmpstr[0];
    //    alert('update');
    //    var clientContext = new SP.ClientContext();
    //    var oList = clientContext.get_web().get_lists().getByTitle('Chart');
    //    this.oListItem = oList.getItemById(tmpstr[1]);
    //    oListItem.set_item('QTY', 2);
    //    oListItem.update();
    //    alert('update 2');
    //    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    //}

    //function onQuerySucceeded() {

    //    alert('Item updated!');
    //}

    //function onQueryFailed(sender, args) {

    //    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    //}



    function Compare(str) {
        //alert(str);
        var Addstr = str;
        var dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        var id = tmpstr[1];
        var clientContext = SP.ClientContext.get_current();

        var list = clientContext.get_web().get_lists().getByTitle('Chart');

        var skillcamlQuery = new SP.CamlQuery();

        //skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Product_x0020_Name'/><Value Type='Text'>" + Productname + "</Value></Eq><Eq><FieldRef Name='Author' LookupId='TRUE'/><Value Type='Lookup'>" + _spPageContextInfo.userId+ "</Value></Eq></And></Where></Query></View>");

        var skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
            if (skillcollListItem.get_count() >= 1) {
                var listItemInfo = '';
                var listItemEnumerator = skillcollListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    str = oListItem.get_id() + "#" + oListItem.get_item('QTY');
                }
                dfd.resolve(str +"#"+ 1);
            }
            else {
                //str = str + "#" + 0;
                dfd.resolve(Addstr + "#" + 0);
            }
        }), Function.createDelegate(this, function (sender, args) {
            dfd.reject(sender, args);
        }));
        return dfd.promise();
    }
</script>

 <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 

I don't understand why you load a item from Hardware Catalog list while the id belongs to chart list.

var oList1 = clientContext.get_web().get_lists().getByTitle('Hardware Catalog');
var oListItem1 = oList1.getItemById(id);
clientContext.load(oListItem1);
alert(oListItem1.get_item('Product_x0020_Name'));
  • lee i have updated the code, the updation seems to be fine but adding the item of the item does not exist is where i am struggling with . – Rinu Dec 6 '17 at 4:55
  • Update the answer. – Lee_MSFT Dec 6 '17 at 5:20
  • I don't understand why you load a item from Hardware Catalog list while the id belongs to chart list. ? . Because i am trying to copy the list item from the Hardware Catalogue to the chart if the item does not exists. it it does i simply update the QTY. What i need is to add the items from the hardware catalogue. to chart . the above code add Some value – Rinu Dec 6 '17 at 5:29
  • the addstr returns the ID of the item of the Hardware Catalogue from compare function as you can see i have copied it in the beginning. i was thinking to use this id to find the item useing getItemID method and add it to the chart hence the below code var oList1 = clientContext.get_web().get_lists().getByTitle('Hardware Catalog'); var oListItem1 = oList1.getItemById(id); clientContext.load(oListItem1); – Rinu Dec 6 '17 at 5:31
  • The compare function get ID from chart list based on your code. – Lee_MSFT Dec 6 '17 at 5:57
0

I was able to get it to work with the below code.

    <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var olist111;
    function AddToCart() {
        var context = SP.ClientContext.get_current();

        var web = context.get_web();
        context.load(web);

        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var oList = context.get_web().get_lists().getByTitle('Chart');
        context.load(oList);

        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        var spItems = [];

        //alert('debut');
        for (i = 0; i < selectedItems.length; i++) {
            var currentItem = source.getItemById(selectedItems[i].id);
            //alert(selectedItems[i].id);
            context.load(currentItem);
            spItems.push(currentItem);

        }


        context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {

            for (var j = 0; j < spItems.length; j++) {
                // pass use ID + product name
                var compareFun = Compare(spItems[j].get_item('Product_x0020_Name') + "#" + spItems[j].get_item('ID'));
                olist111 = spItems;
                compareFun.done(function (data) {
                    var Match = data;
                    var tempstr = Match.split("#");
                    var compared = tempstr[2];

                    if (compared == '1') {
                        //alert("Item already exists in the chart");

                        var tmpstr = Match.split("#");

                        var id = tmpstr[0];
                        var qty = tmpstr[1];
                        var clientContext = new SP.ClientContext();
                        var oList1 = clientContext.get_web().get_lists().getByTitle('Chart');
                        var oListItem = oList1.getItemById(id);

                        oListItem.set_item('QTY', parseInt(qty) + 1);
                        oListItem.update();
                        clientContext.executeQueryAsync(success, failed);
                    }

                    else if (compared == '0') {
                        alert("Item not in the chart");
                        var tmpstr = Match.split("#");

                        var Productname = tmpstr[0];
                        var id = tmpstr[1];
                       // alert(id);
                        var clientContext1 = new SP.ClientContext.get_current();

                        var oList1 = clientContext1.get_web().get_lists().getByTitle('Hardware Catalog');
                        var oListItem1 = oList1.getItemById(id);
                        clientContext1.load(oListItem1);
                        clientContext1.executeQueryAsync(success1, failed1);
                       // clientContext.load(oListItem1);
                       // alert(111);
                       // alert(oListItem1.get_item('Product_x0020_Name'));

                        function success1(sender, args) {
                           // alert('in ');
                            // window.location.reload(true);

                            var clientContext = new SP.ClientContext();
                            var oList = clientContext.get_web().get_lists().getByTitle('Chart');
                            //alert(112);
                            //var chartListitem = oList.getItemById(id);
                            clientContext.load(oList);
                            var itemCreateInfo = new SP.ListItemCreationInformation();
                            var oListItem = oList.addItem(itemCreateInfo);
                            //alert(112);
                            oListItem.set_item('Title', oListItem1.get_item('Product_x0020_Name'));
                            //alert(113);
                            oListItem.set_item('Item', oListItem1.get_item('Item'));
                            oListItem.set_item('Product_x0020_Name', oListItem1.get_item('Product_x0020_Name'));
                            oListItem.set_item('Description', oListItem1.get_item('Description'));
                            oListItem.set_item('Price', oListItem1.get_item('Price'));
                            oListItem.set_item('Department', oListItem1.get_item('Department'));
                            oListItem.set_item('Branch', oListItem1.get_item('Branch'));
                            oListItem.update();
                            //alert(114);
                            clientContext.executeQueryAsync(success, failed);

                            //window.location.href = window.location.href;
                        }
                        function failed1(sender, args) {
                            alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
                            // window.location.reload(true);
                            window.location.href = window.location.href;
                        }

                        //var oList = clientContext.get_web().get_lists().getByTitle('Chart');
                        //alert(112);
                        ////var chartListitem = oList.getItemById(id);
                        //clientContext.load(oList);
                        //var itemCreateInfo = new SP.ListItemCreationInformation();
                        //var oListItem = oList.addItem(itemCreateInfo);
                        //alert(112);
                        //oListItem.set_item('Title', oListItem1.get_item('Product_x0020_Name'));
                        //alert(113);
                        //oListItem.set_item('Item', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.set_item('Product_x0020_Name', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.set_item('Description', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.set_item('Price', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.set_item('Department', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.set_item('Branch', oListItem1.get_item('Product_x0020_Name'));
                        //oListItem.update();
                        //clientContext.executeQueryAsync(success, failed);

                    }
                }).fail(function (sender, args) { alert('Failed'); });
                console.log("index" + spItems[j].get_item('Product_x0020_Name'));
            }
            function Succeeded() {

                alert('Item updated!');
            }
            //context.executeQueryAsync(success);
            //   alert('done');

        }), Function.createDelegate(this, error));
        function onQuerySucceeded(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function onQueryFailed(sender, args) {
            alert('Failed to add!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        //function success1(sender, args) {
        //    alert('in ');
        //    // window.location.reload(true);

        //    var clientContext = new SP.ClientContext();
        //    var oList = clientContext.get_web().get_lists().getByTitle('Chart');
        //    alert(112);
        //    //var chartListitem = oList.getItemById(id);
        //    clientContext.load(oList);
        //    var itemCreateInfo = new SP.ListItemCreationInformation();
        //    var oListItem = oList.addItem(itemCreateInfo);
        //    alert(112);
        //    oListItem.set_item('Title', oListItem1.get_item('Product_x0020_Name'));
        //    alert(113);
        //    oListItem.set_item('Item', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.set_item('Product_x0020_Name', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.set_item('Description', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.set_item('Price', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.set_item('Department', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.set_item('Branch', oListItem1.get_item('Product_x0020_Name'));
        //    oListItem.update();
        //    clientContext.executeQueryAsync(success, failed);

        //    window.location.href = window.location.href;
        //}
        //function failed1(sender, args) {
        //    alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
        //    // window.location.reload(true);
        //    window.location.href = window.location.href;
        //}

        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }
        function failed(sender, args) {
            alert('Unable to update!' + args.get_message() + '\n' + args.get_stackTrace());
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function Succeeded() {

            alert('Item updated!');
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function success(sender, args) {
            alert('Added to chart!');
            // window.location.reload(true);
            window.location.href = window.location.href;
        }

        function error(sender, args) { alert('error'); }
    }

    //function updateListItem(str) {
    //    alert(1);
    //    alert(str);
    //    var tmpstr = str.split("#");
    //    alert(tmpstr[0]);
    //    alert(tmpstr[1]);
    //    var Productname = tmpstr[0];
    //    alert('update');
    //    var clientContext = new SP.ClientContext();
    //    var oList = clientContext.get_web().get_lists().getByTitle('Chart');
    //    this.oListItem = oList.getItemById(tmpstr[1]);
    //    oListItem.set_item('QTY', 2);
    //    oListItem.update();
    //    alert('update 2');
    //    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    //}

    //function onQuerySucceeded() {

    //    alert('Item updated!');
    //}

    //function onQueryFailed(sender, args) {

    //    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    //}



    function Compare(str) {
        //alert(str);
        var Addstr = str;
        var dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        var id = tmpstr[1];
        var clientContext = SP.ClientContext.get_current();

        var list = clientContext.get_web().get_lists().getByTitle('Chart');

        var skillcamlQuery = new SP.CamlQuery();

        //skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        skillcamlQuery.set_viewXml("<View><Query> <Where><And><Eq> <FieldRef Name='Product_x0020_Name'/><Value Type='Text'>" + Productname + "</Value></Eq><Eq><FieldRef Name='Author' /><Value Type='Lookup'>" + _spPageContextInfo.userDisplayName + "</Value> </Eq> </And> </Where></Query></View>");

        var skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
            if (skillcollListItem.get_count() >= 1) {
                var listItemInfo = '';
                var listItemEnumerator = skillcollListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    str = oListItem.get_id() + "#" + oListItem.get_item('QTY');
                }
                dfd.resolve(str + "#" + 1);
            }
            else {
                //str = str + "#" + 0;
                dfd.resolve(Addstr + "#" + 0);
            }
        }), Function.createDelegate(this, function (sender, args) {
            dfd.reject(sender, args);
        }));
        return dfd.promise();
    }
</script>

 <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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