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);
New contributor
Hellsings is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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:

  • 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

Hellsings is a new contributor. Be nice, and check out our Code of Conduct.

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.