Whenever I call executeQuereyAsync
after an update()
, it does not run either the success or failure methods. Elsewhere in my code, I call it after load()
and it works fine.
So this works fine in my code, alerting "this worked!":
clientContext.load(oListItem, "holderListID");
clientContext.executeQueryAsync(
function(sender, args) { // if query succeeds...
alert('this worked!');
}
},
function(sender, args) { // if query fails...
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
While this correctly updates oListItem
but does not alert anything, skipping past to the next line of code:
function updateThings() {
//run set_items() on things
oListItem.update();
clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert('It works!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Any suggestions would be appreciated!
I am creating a simple checkout/return system. Here is my code for checking an item out, which includes my current workaround for not being able to access the success callback. As it is written, this code works, but the success/failure functions after update()
are never called:
function clickCheckout(){
state = 0;
checkout = 1;
try{
getId();
isCheckedOut();
}
catch(err) {
alert("Please try again, checkout failed");
}
}
function getId() {
clientContext = new SP.ClientContext.get_current();
oListItem = clientContext.get_web().get_lists().getByTitle('Equipment Checkout').getItemById(listItemID);
}
function isCheckedOut() {
clientContext.load(oListItem, holderListID);
clientContext.executeQueryAsync(
function(sender, args) { // if query succeeds...
oldUsername = oListItem.get_item(holderListID).get_lookupValue();
if(oldUsername != 'Not in use') {
if(checkout == 1) {
alert("Cannot check out: item is currently checked out by " + oldUsername);
} else {
returnItem();
}
} else {
if(checkout == 0) {
alert("Item was not checked out!");
} else {
checkoutItem();
}
}
},
function(sender, args) { // if query fails...
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
}
function checkoutItem() {
var today = new Date();
oListItem.set_item(dateTimeID, today);
var user = new SP.FieldUserValue();
user.set_lookupId(_spPageContextInfo.userId);
oListItem.set_item(holderListID, user);
oListItem.update();
clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
setTimeout(setMoreInfo,300); // workaround to allow executeQueryAsync() to finish
}
function setMoreInfo() { // continuing from the workaround
clientContext.load(oListItem, holderListID);
clientContext.executeQueryAsync(
function(sender, args) { // if query succeeds...
newUsername = oListItem.get_item(holderListID).get_lookupValue();
oListItem.set_item(holderUsernameListID, newUsername);
oListItem.update();
clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
alert("Item is successfully checked out!");
redirectToView(); // sends user to another page
},
function(sender, args) { // if query fails...
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
}
function onQuerySucceeded(sender, args) {
alert('It works!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
.load()
after making an update. It might be required. Though, I see a lot of examples doing updates without the.load()
, and I imagine they can't all be wrong either.alert
use and learn to useconsole.log(x)
to the F12 console. Only then (when you code is better readable) can you do some serious debugging of your coding error. At first glance you are going wrong with the scope ofclientContext