1

In SharePoint 2013, "Discussion" list, I have created a button "fix/unfix" (Yes/No type field) discussion. But the script only works the second time and subsequent times. This script checks the value 0 or 1 and changes to the opposite. I'm not very good at scripting, so please show me what is my mistake.

function fieldTest() {
          var ctx = SP.ClientContext.get_current();
          var olist = ctx.get_web().get_lists().getByTitle('discussion');
          var oitem = olist.getItemById(1);
          ctx.load(oitem, "fix");
          ctx.executeQueryAsync(function () {
                    check = oitem.get_item("fix");
                    if (!check) {
                             oitem.set_item("fix", true);
                    }
                    else {
                             oitem.set_item("fix", false);
                    }
                    oitem.update();    
                    });  
}
0

If you add another executeQuery after oitem.update(); this will work.

The reason it works on the second+ run is because the queued update then will be part of your initial executeQuery!

Modified, the end of your script should look like this:

                    oitem.update();    
                    ctx.executeQueryAsync();
                    });  
}
| improve this answer | |
  • 1
    Great! It all worked! I could not even imagine it, what i can use "ctx.executeQueryAsync ()" again. 2 days I rack my brains. Thank you very much, I will take note of this! – Hellsings Mar 25 at 16:52
  • Glad to be of help! – Rune Sperre Mar 25 at 17:36

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.