1

I've got a function that checks if a list item (setting) is true and then changes the setting which it does correctly but doesn't execute the second ExecuteQueryAsync functions. In debugging it jumps out of the code after that function/line and never reaches the OnQuerySuccess/Failure Here's what I have:

<html>
<head>
<script type="text/JavaScript">
<!--
function loopScript(){
    setInterval(checkSetting, 5000);
}
function checkSetting() {
 var clientContext = SP.ClientContext.get_current();
 var oList = clientContext.get_web().get_lists().getByTitle('Biz.WebPartSettings');
 var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<Query> \
   <Where> \
         <Eq> \
            <FieldRef Name='Title' /> \
            <Value Type='Text'>PS-ImageSliderRefresh-ImageSlider</Value> \
         </Eq> \
   </Where> \
</Query>");
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

function onQuerySucceeded(sender, args) {

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
    //console.log(oListItem.get_fieldValues());  
        if(oListItem.get_item('Setting_x0020_Value') == 'Yes'){
            oListItem.set_item('Setting_x0020_Value', 'No');
                console.log(oListItem.get_fieldValues());  
                oListItem.update();

        clientContext.executeQueryAsync(Function.createDelegate(
            this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailure));

        function onQuerySuccess(sender, args){
            alert('Setting changed');
            location.reload(true);
        }

        function onQueryFailure(sender, args) {
                alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        }
    }
}

function onQueryFailed(sender, args) {

    alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


//   -->
</script>
</head>
<body onload="JavaScript:loopScript();">
</body>
</html>

2 Answers 2

2

Try with creating onQuerySuccess and onQueryFailure functions outside of onQuerySucceeded function.

function onQuerySucceeded(sender, args) {

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
    //console.log(oListItem.get_fieldValues());  
        if(oListItem.get_item('Setting_x0020_Value') == 'Yes'){
            oListItem.set_item('Setting_x0020_Value', 'No');
                console.log(oListItem.get_fieldValues());  
                oListItem.update();

        clientContext.executeQueryAsync(Function.createDelegate(
            this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailure));


        }
    }
}

function onQuerySuccess(sender, args){
            alert('Setting changed');
            location.reload(true);
        }

        function onQueryFailure(sender, args) {
                alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }

OR call the clientContext.executeQueryAsync() function like this:

clientContext.executeQueryAsync(function onQuerySuccess(sender, args){
                                   alert('Setting changed');
                                   location.reload(true);
                                 }, function onQueryFailure(sender, args){
                                    alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                                 }
                                );
0

Probably because (oListItem.get_item('Setting_x0020_Value') == 'Yes') isn't true when you run the code.

If you straighten out all of the indentation levels using an IDE of some sort you'll get a clearer picture on where it falls out:

function onQuerySucceeded(sender, args) {

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        //console.log(oListItem.get_fieldValues());  
        if (oListItem.get_item('Setting_x0020_Value') == 'Yes') {
            oListItem.set_item('Setting_x0020_Value', 'No');
            console.log(oListItem.get_fieldValues());
            oListItem.update();

            clientContext.executeQueryAsync(Function.createDelegate(
                this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailure));

            function onQuerySuccess(sender, args) {
                location.reload(true);
            }

            function onQueryFailure(sender, args) {
                alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        } // end if (oListItem.get_item('Setting_x0020_Value') == 'Yes')
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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