javascriptsharepointsharepoint-onlineweb-partschoicefield

Change drop-down choice value on SharePoint column using JavaScript


I've been assigned to work from a popup (activated from a button on a Sharepoint page/"dashboard") window which opens a new form from a SharePoint list. This is what the pop up looks like.

SharePoint New Form for Event XYZ

Note: The Promo Item, Quantity Purchased, Event, and Delete (the flag!!!) are all modified using the SharePoint new form. These inputs are all in their own columns in a SharePoint list (looking at another perspective). When the item is saved, this happens:

Saved Toy for Event XYZ

Note: When the toy item is saved/created after SAVE, it's saved to another SharePoint list for record keeping.

Now, please pay attention to the Delete button. When the toy item is created, the Delete flag is defaulted to NO. Objective: I want to click the delete button and change the Sharepoint choice values from NO to YES. So far, I have created this code so far (it does not work):

function deleteGuestName(parent, parentID){
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Toy Order List'); //SP list name 

        this.oListItem = oList.getItemById(listID); 
        this.oListItem = oList.set_item("Delete","Yes"); //I thought this would change the choice field
        oList.update();
        oListItem.deleteObject();

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

function onQuerySucceeded() {
refreshGrid();
    parent.closeHSPopup();
}

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

Any help on this is appreciated. I will be happy to clarify if needed. thnxx


Solution

  • Here are doubts for you code.

    this.oListItem = oList.getItemById(listID); 
    

    To get list item, oList.getItemById(listID) should be oList.getItemById(itemid)

    To update list item, oList.update() should be

    this.oListItem.set_item("Delete","Yes")
    this.oListItem.update()
    

    And why you call oListItem.deleteObject() to delete item if you want to update the item?