How to Set Yes/No field on selected list items (if/else) using JavaScript in SharePoint 2013

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/278081

  •  08-02-2021
  •  | 
  •  

Question

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();    
                    });  
}
Was it helpful?

Solution

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();
                    });  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top