4

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());
}
  • can you post full code ... any be some error within the upper part of code. – Monica Jagani May 30 '17 at 12:57
  • Example from MSDN shows calling .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. – wjervis May 30 '17 at 13:07
  • @MonicaJagani I just added most of my code. – Tom Ato May 30 '17 at 14:00
  • @wjervis I just tried that, and nothing changed :/ – Tom Ato May 30 '17 at 14:01
  • Clean up your code and ditch that IE8 syntax, see here, then ditch all that alert use and learn to use console.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 of clientContext – Danny '365CSI' Engelman May 30 '17 at 14:42
0

if you call the this.onQuerySucceeded, that function has to be outside of the function you are calling executeQueryAsync(). So your last example is a matter of breaking the onQuerySucceeded and onQueryFailed outside of the function calling the execute.

If you are creating a list item, you need to load the item variable in the clientContext first using clientContext.load(oListItem);

  • I just added more complete code; the success/fail functions are outside of the function calling executeQueryAsync. I am only modifying list items, not creating new ones, but thanks! – Tom Ato May 30 '17 at 14:00
  • That 'has to be outside' depends on what the this scope is. In most code examples that is the window object, then yes, 'outside' is the global scope. – Danny '365CSI' Engelman May 30 '17 at 14:39
0

After cleaning up my code as per the suggestions of the comments above and the info in the answer here, it seems to be working.

I removed the apparently outdated Function.createDelegate

and simply using

oListItem.update();
clientContext.executeQueryAsync( onQuerySucceeded, 
                                 onQueryFailed);

or putting the functions directly as the callbacks works for me now. It seems using this was having issues because of the scope of my clientContext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.