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