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" />