I am running a function on page load to update list item. Here's my complete code -
jQuery(document).ready(function(){
updatePageViewCount();
});
function updatePageViewCount() {
var clContext = new SP.ClientContext.get_current();
var topicList = clContext.get_web().get_lists().getByTitle('Topics');
clContext.load(topicList);
clContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFail));
}
function onQuerySuccess() {
var listItemEnum = topicList.getEnumerator();
while (listItemEnum.moveNext()) {
var topicListItem = listItemEnum.get_current();
var url = window.location.href;
alert(url);
var topicId = url.split("ID=")[1].split("&")[0];
alert(topicId);
if(topicListItem.get_Item('Topic_x0020_ID') == topicId){
var oldData = topicListItem.get_item('ViewCount');
var newData = parseInt(oldData) + 1;
alert(newData);
topicListItem.set_item('ViewCount', newData);
topicListItem.update();
clContext.executeQueryAsync(Function.createDelegate(this, this.onItemUpdated), Function.createDelegate(this, this.onQueryFail));
}
}
}
function onItemUpdated() {
alert('Item has been updated!');
}
function onQueryFail(sender, args) {
alert('Request failed.');
}
Can anyone help me find a fix for this?