0

I am moving selected item from a list to another.

i want to increase the quantity of a field it the item already exists. Please see my existing code is below.

I add or update the list based on the comparison clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

is not getting called?

    <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;

                    if (typeof Match === 'string' || Match instanceof String) {
                        alert("Item already exists in the chart");
                        alert(Match);
                        var tmpstr = Match.split("#");
                        alert(tmpstr[0]);
                        alert(tmpstr[1]);
                        var Productname = tmpstr[0];

                        var clientContext = new SP.ClientContext();
                        alert('Updated');
                        var oList1 = clientContext.get_web().get_lists().getByTitle('Chart');
                        alert("Updated 2");
                        var oListItem = oList1.getItemById(tmpstr[1]);
                        alert("Updated 3");
                        oListItem.set_item('QTY', 2);
                        oListItem.update();
                        alert('Updated');
                        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

                    }

                    else if (Match == 0) {
                        alert("Item not in the chart");
                    }
                }).fail(function (sender, args) { alert('Failed'); });
                console.log("index" + spItems[j].get_item('Product_x0020_Name'));
            }
            function onQuerySucceeded() {

                alert('Item updated!');
            }

            function onQueryFailed(sender, args) {

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

        }), Function.createDelegate(this, error));


        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];

        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();
        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 dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        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) {
                dfd.resolve(str);
            }
            else {
                dfd.resolve(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" /> 

1 Answer 1

0
  1. Set Title as unique field.(in your case I think it will be product name)
  2. write a function to check whether same item exists or not.(can be easily achieved by JSOM or REST) Ref this for code e.g.
  3. If exist, update QTY of same item.
  4. If not exist, create new. (which you already have done.)
6
  • so in the above code instead of thosiba i can use the code from your example to compare ?
    – Rinu
    Commented Nov 29, 2017 at 10:40
  • i have updated the code as suggested but it still adds the item
    – Rinu
    Commented Nov 29, 2017 at 11:15
  • debugg your Compare function and check whats getting in skillcollListItem
    – Anil Pal
    Commented Nov 29, 2017 at 11:38
  • i think executeQueryAsync cannot return a value or when the call is made after the clientContext.load(skillcollListItem); the control goes back to the main function hence the values are not returned ?
    – Rinu
    Commented Nov 29, 2017 at 12:46
  • You can use deferred to hold the values. Ref this api.jquery.com/category/deferred-object
    – Anil Pal
    Commented Nov 30, 2017 at 10:24

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.