Skip to main content
added 1340 characters in body
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61

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 dfd = $.Deferred();olist111;
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        var web = context.get_web();
        context.load(web);
        alert('1');
        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var dfd = $.Deferred();
        alert('debut');
        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++) {
                //alert(pItems[j].get_item('Title')); pass use ID + product name
                var MatchcompareFun = Compare(spItems[j].get_item('Product_x0020_Name'));
               + alert(Match"#" + "Match variable"spItems[j].get_item('ID'));
                if (Match ==olist111 1)= {spItems;
                    alertcompareFun.done("Item already exists in thefunction chart"(data); {
                    oListItem.set_item('QTY',var spItems[j].get_item('QTY')Match += 1);data;
                    oListItem.update();
                }
            if (typeof Match === else'string' if|| (Match ==instanceof 0String) {
                    alert("Item not in the chart");
                    var itemCreateInfo = new SP.ListItemCreationInformationalert();
                  "Item already varexists oListItemin =the oList.addItem(itemCreateInfochart");
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title')Match);
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                  var tmpstr oListItem.set_item('Product_x0020_Name',= spItems[j]Match.get_itemsplit('Product_x0020_Name')"#");
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_itemalert('Price')tmpstr[0]);
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_itemalert('Branch')tmpstr[1]);
                    oListItem.update();
             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', spItems[j]2);
                        oListItem.get_itemupdate('QTY');
 + 1                      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(spItems[j].get_item('Product_x0020_Name'));
               'Item //oList.update(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('Comparestr);
 : ' +     var dfd = $.Deferred();
        var tmpstr = str.split("#");
        var Productname = tmpstr[0];
        var clientContext = SP.ClientContext.get_current();
        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>");
       var this.skillcollListItemlist = listclientContext.getItemsget_web(skillcamlQuery);

        clientContext.loadget_lists(skillcollListItem).getByTitle('Chart');

        var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParamskillcamlQuery = onValidateClientSucceeded(); }),new FunctionSP.createDelegate(this, function () { _returnParam = onValidateClientFailedCamlQuery(); }));

        //skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef alertName='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        skillcamlQuery.set_viewXml('compare"<View><Query> 1'<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>");

        function onValidateClientSucceeded(sender, args) {
        var skillcollListItem = dfdlist.resolvegetItems(sender, argsskillcamlQuery);

        clientContext.load(skillcollListItem);

          clientContext.executeQueryAsync(Function.createDelegate(this, function alert("Comparesender, successful"args); {
            if (skillcollListItem.get_count() >= 1) {

                 alertdfd.resolve('match found'str);
                return 1
            }
            else {
                alertdfd.resolve('mach not found'0);
                return 0
            }
        }
      ), Function.createDelegate(this, function onValidateClientFailed(sender, args) {
            dfd.resolvereject(sender, args);
 
        }));
        return dfd.promise();
    }
    </script>
    
 <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 

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

  <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    //var dfd = $.Deferred();
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        var web = context.get_web();
        context.load(web);
        alert('1');
        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var dfd = $.Deferred();
        alert('debut');
        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++) {
                //alert(pItems[j].get_item('Title'));
                var Match = Compare(spItems[j].get_item('Product_x0020_Name'));
                alert(Match + "Match variable");
                if (Match == 1) {
                    alert("Item already exists in the chart");
                    oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                    oListItem.update();
                }
                else if (Match == 0) {
                    alert("Item not in the chart");
                    var itemCreateInfo = new SP.ListItemCreationInformation();
                    var oListItem = oList.addItem(itemCreateInfo);
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title'));
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                    oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_item('Price'));
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                    oListItem.update();
                }

                // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);


                //alert(spItems[j].get_item('Product_x0020_Name'));
                //oList.update();                
            }
            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 Compare(str) {
        alert('Compare : ' + str);
        var clientContext = SP.ClientContext.get_current();
        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>");
        this.skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(); }), Function.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

       // alert('compare 1');

        function onValidateClientSucceeded(sender, args) {
           dfd.resolve(sender, args);


            alert("Compare successful");
            if (skillcollListItem.get_count() >= 1) {

                 alert('match found');
                return 1
            }
            else {
                alert('mach not found');
                return 0
            }
        }
        function onValidateClientFailed(sender, args) {
            dfd.resolve(sender, args);
 
        }
    }
    </script>
    <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 

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" /> 
added 447 characters in body
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61
  <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    //var dfd = $.Deferred();
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        var web = context.get_web();
        context.load(web);
        alert('1');
        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var sourceIddfd = SP.ListOperation.Selection$.getSelectedListDeferred();
        alert('debut');
        var sourceoList = webcontext.get_web().get_lists().getByIdgetByTitle(sourceId'Chart');
        context.load(oList);

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

    var oList     alert('debut');
        for (i = context0; i < selectedItems.get_web(length; i++) {
            var currentItem = source.get_listsgetItemById()selectedItems[i].getByTitleid);
            //alert('Chart'selectedItems[i].id);
            context.load(oListcurrentItem);
            spItems.push(currentItem);

    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    var spItems = [];
    //  alert('debut');
    for (i = 0; i < selectedItems.length; i++) {}
        var currentItem = sourcecontext.getItemByIdexecuteQueryAsync(selectedItems[i]Function.id);
        //alertcreateDelegate(selectedItems[i].id);
      this, function context.load(currentItem);
       sender, spItems.push(currentItemargs); {

    }
    context.executeQueryAsync(Function.createDelegate(this, function   for (sender,var argsj = 0; j < spItems.length; j++) {
                // alert('test'pItems[j].get_item('Title'));
        for (var j = 0; j < spItems.length; j++)var {Match = Compare(spItems[j].get_item('Product_x0020_Name'));
            if (Compare( spItems[j].get_item  alert('Product_x0020_Name')Match )==1+ "Match variable");
                if (Match == 1) {
                    alert("Item already exists in the chart");
                    oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                    oListItem.update();
            }
    }
        else
        else if (Match == 0) {
                    alert("Item not in the chart");
                    var itemCreateInfo = new SP.ListItemCreationInformation();
                    var oListItem = oList.addItem(itemCreateInfo);
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title'));
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                    oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_item('Price'));
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                    oListItem.update();
                }

                // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);


                //alert(spItems[j].get_item('Product_x0020_Name'));
                //oList.update();                
            }
            context.executeQueryAsync(success);
            // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1alert('done');

           
            //alert(spItems[j].get_item('Product_x0020_Name')});
           , //oListFunction.updatecreateDelegate();                
        }
       this, context.executeQueryAsync(successerror);
        //   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 successerror(sender, args) {
        alert('Added to chart!');
        // window.location.reload(true'error');
        window.location.href = window.location.href;}
    }

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

    function Compare(str)
  {
        alert('Compare : ' + str);
        var clientContext = SP.ClientContext.get_current();
        list = clientContext.get_web().get_lists().getByTitle('Chart');
        var skillcamlQuery = new SP.CamlQuery();
    //skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq><Eq><FieldRef Name='BusinessDomain' /><Value Type='Text'>"+VariableName+"</Value></Eq></And></Where></Query></View>");

        skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
        this.skillcollListItem = list.getItems(skillcamlQuery);
    //clientContext.load(list);         
    clientContext.load(skillcollListItem);

    var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(); }), FunctionclientContext.createDelegate(this, function () { _returnParam = onValidateClientFailedload(); })skillcollListItem);

        var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(sender); }), argsFunction.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

        var flg// =alert('compare 0;1');

        alert("Compare successful");
        if (skillcollListItem.get_count() >= 1) {
            flg = 1;
           function alertonValidateClientSucceeded('matchsender, found'args);
            return 1
        }
        else {
            alertdfd.resolve('mach notsender, found'args);
            return 0
        }


    }        alert("Compare successful");
            if (skillcollListItem.get_count() >= 1) {

                alert('match found');
                return 1
            }
            else {
                alert('mach not found');
                return 0
            }
        }
        function onValidateClientFailed(sender, args) {
            dfd.resolve(sender, args);

        }
    }
    </script>
    <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
<script type="text/javascript">
function AddToCart() {
    var context = SP.ClientContext.get_current();
    //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
    //you don't use clientContext
    //var clientContext = new SP.ClientContext(siteUrl);
    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) {
        // alert('test');
        for (var j = 0; j < spItems.length; j++) {
            if (Compare( spItems[j].get_item('Product_x0020_Name') )==1)
            {
                alert("Item already exists in the chart");
                oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                oListItem.update();
            }
            else
            {
                alert("Item not in the chart");
                var itemCreateInfo = new SP.ListItemCreationInformation();
                var oListItem = oList.addItem(itemCreateInfo);
                oListItem.set_item('Title', spItems[j].get_item('Title'));
                //alert(spItems[j].get_item('Title'));
                oListItem.set_item('Item', spItems[j].get_item('Item'));
                oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                oListItem.set_item('Description', spItems[j].get_item('Description'));
                oListItem.set_item('Price', spItems[j].get_item('Price'));
                oListItem.set_item('Department', spItems[j].get_item('Department'));
                oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                oListItem.update();
            }
           
           // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);

           
            //alert(spItems[j].get_item('Product_x0020_Name'));
            //oList.update();                
        }
        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 Compare(str)
 {
    alert('Compare : ' + str);
    var clientContext = SP.ClientContext.get_current();
    list = clientContext.get_web().get_lists().getByTitle('Chart');
    var skillcamlQuery = new SP.CamlQuery();
    //skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq><Eq><FieldRef Name='BusinessDomain' /><Value Type='Text'>"+VariableName+"</Value></Eq></And></Where></Query></View>");

    skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
    this.skillcollListItem = list.getItems(skillcamlQuery);
    //clientContext.load(list);         
    clientContext.load(skillcollListItem);

    var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(); }), Function.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

    function onValidateClientSucceeded(sender, args) {

        var flg = 0;

        alert("Compare successful");
        if (skillcollListItem.get_count() >= 1) {
            flg = 1;
            alert('match found');
            return 1
        }
        else {
            alert('mach not found');
            return 0
        }


    }


}
</script>
<input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
  <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    //var dfd = $.Deferred();
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        var web = context.get_web();
        context.load(web);
        alert('1');
        var sourceId = SP.ListOperation.Selection.getSelectedList();
        var source = web.get_lists().getById(sourceId);

        var dfd = $.Deferred();
        alert('debut');
        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++) {
                //alert(pItems[j].get_item('Title'));
                var Match = Compare(spItems[j].get_item('Product_x0020_Name'));
                alert(Match + "Match variable");
                if (Match == 1) {
                    alert("Item already exists in the chart");
                    oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                    oListItem.update();
                }
                else if (Match == 0) {
                    alert("Item not in the chart");
                    var itemCreateInfo = new SP.ListItemCreationInformation();
                    var oListItem = oList.addItem(itemCreateInfo);
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title'));
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                    oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_item('Price'));
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                    oListItem.update();
                }

                // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);


                //alert(spItems[j].get_item('Product_x0020_Name'));
                //oList.update();                
            }
            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 Compare(str) {
        alert('Compare : ' + str);
        var clientContext = SP.ClientContext.get_current();
        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>");
        this.skillcollListItem = list.getItems(skillcamlQuery);

        clientContext.load(skillcollListItem);

        var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(); }), Function.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

       // alert('compare 1');

        function onValidateClientSucceeded(sender, args) {
           dfd.resolve(sender, args);


            alert("Compare successful");
            if (skillcollListItem.get_count() >= 1) {

                alert('match found');
                return 1
            }
            else {
                alert('mach not found');
                return 0
            }
        }
        function onValidateClientFailed(sender, args) {
            dfd.resolve(sender, args);

        }
    }
    </script>
    <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
added 1510 characters in body
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61
   <script type="text/javascript">
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        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) {
            // alert('test');
            for (var j = 0; j < spItems.length; j++) {
                if (Compare( spItems[j].get_item('Product_x0020_Name') == "Thoshiba")==1)
                {
                    alert("Item already exists in the chart");
                }oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                elseoListItem.update();
            }
            else
            {
                alert("Item not in the chart");
                var itemCreateInfo = new SP.ListItemCreationInformation();
                    var oListItem = oList.addItem(itemCreateInfo);
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title'));
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                    oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_item('Price'));
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                }oListItem.update();
            }
   
          
           // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);

                oListItem.update();
                //alert(spItems[j].get_item('Product_x0020_Name'));
                //oList.update();                
            }
            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 Compare(str)
{
    alert('Compare : ' + str);
    var clientContext = SP.ClientContext.get_current();
    list = clientContext.get_web().get_lists().getByTitle('Chart');
    var skillcamlQuery = new SP.CamlQuery();
    //skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq><Eq><FieldRef Name='BusinessDomain' /><Value Type='Text'>"+VariableName+"</Value></Eq></And></Where></Query></View>");

    skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
    this.skillcollListItem = list.getItems(skillcamlQuery);
    //clientContext.load(list);         
    clientContext.load(skillcollListItem);

    var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function error() { _returnParam = onValidateClientSucceeded(); }), Function.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

    function onValidateClientSucceeded(sender, args) {

        var flg = 0;

        alert('error'"Compare successful"); 
 }       if (skillcollListItem.get_count() >= 1) {
            flg = 1;
            alert('match found');
            return 1
        }
    </script>    else {
            alert('mach not found');
            return 0
        }


    }


}
</script>
<input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
   <script type="text/javascript">
    function AddToCart() {
        var context = SP.ClientContext.get_current();
        //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
        //you don't use clientContext
        //var clientContext = new SP.ClientContext(siteUrl);
        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) {
            // alert('test');
            for (var j = 0; j < spItems.length; j++) {
                if (spItems[j].get_item('Product_x0020_Name') == "Thoshiba")
                {
                    alert("Item already exists in the chart");
                }
                else
                {
                    var itemCreateInfo = new SP.ListItemCreationInformation();
                    var oListItem = oList.addItem(itemCreateInfo);
                    oListItem.set_item('Title', spItems[j].get_item('Title'));
                    //alert(spItems[j].get_item('Title'));
                    oListItem.set_item('Item', spItems[j].get_item('Item'));
                    oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                    oListItem.set_item('Description', spItems[j].get_item('Description'));
                    oListItem.set_item('Price', spItems[j].get_item('Price'));
                    oListItem.set_item('Department', spItems[j].get_item('Department'));
                    oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                }
               
                // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);

                oListItem.update();
                //alert(spItems[j].get_item('Product_x0020_Name'));
                //oList.update();                
            }
            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'); }
    }
    </script>
    <input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
<script type="text/javascript">
function AddToCart() {
    var context = SP.ClientContext.get_current();
    //var siteUrl = 'https://xxx.sharepoint.com/sites/Developer';
    //you don't use clientContext
    //var clientContext = new SP.ClientContext(siteUrl);
    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) {
        // alert('test');
        for (var j = 0; j < spItems.length; j++) {
            if (Compare( spItems[j].get_item('Product_x0020_Name') )==1)
            {
                alert("Item already exists in the chart");
                oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);
                oListItem.update();
            }
            else
            {
                alert("Item not in the chart");
                var itemCreateInfo = new SP.ListItemCreationInformation();
                var oListItem = oList.addItem(itemCreateInfo);
                oListItem.set_item('Title', spItems[j].get_item('Title'));
                //alert(spItems[j].get_item('Title'));
                oListItem.set_item('Item', spItems[j].get_item('Item'));
                oListItem.set_item('Product_x0020_Name', spItems[j].get_item('Product_x0020_Name'));
                oListItem.set_item('Description', spItems[j].get_item('Description'));
                oListItem.set_item('Price', spItems[j].get_item('Price'));
                oListItem.set_item('Department', spItems[j].get_item('Department'));
                oListItem.set_item('Branch', spItems[j].get_item('Branch'));
                oListItem.update();
            }
            
           // oListItem.set_item('QTY', spItems[j].get_item('QTY') + 1);

           
            //alert(spItems[j].get_item('Product_x0020_Name'));
            //oList.update();                
        }
        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 Compare(str)
{
    alert('Compare : ' + str);
    var clientContext = SP.ClientContext.get_current();
    list = clientContext.get_web().get_lists().getByTitle('Chart');
    var skillcamlQuery = new SP.CamlQuery();
    //skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq><Eq><FieldRef Name='BusinessDomain' /><Value Type='Text'>"+VariableName+"</Value></Eq></And></Where></Query></View>");

    skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Product_x0020_Name' /><Value Type='Text'>" + str + "</Value></Eq></Where></Query></View>");
    this.skillcollListItem = list.getItems(skillcamlQuery);
    //clientContext.load(list);         
    clientContext.load(skillcollListItem);

    var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onValidateClientSucceeded(); }), Function.createDelegate(this, function () { _returnParam = onValidateClientFailed(); }));

    function onValidateClientSucceeded(sender, args) {

        var flg = 0;

        alert("Compare successful"); 
        if (skillcollListItem.get_count() >= 1) {
            flg = 1;
            alert('match found');
            return 1
        }
        else {
            alert('mach not found');
            return 0
        }


    }


}
</script>
<input name="btnApprove" onclick="AddToCart()" type="button" value="Add To Cart" /> 
added 402 characters in body
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61
Loading
added 1 character in body; edited title
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61
Loading
Source Link
Rinu
  • 1.5k
  • 3
  • 29
  • 61
Loading