Question

I'm trying to add a new record and get the new ID created then use the new ID to create a specific reference number using the following code.

function saveDraftGHrecord() {
ghList = web.get_lists().getByTitle('Listname');
var itemCreateInfo = new SP.ListItemCreationInformation();
ghItem = ghList.addItem(itemCreateInfo);
ghItem.set_item("ReasonOther", $('#txtReasonOther').val());
etc
etc

ghItem.update();
context.load(ghList);

context.executeQueryAsync(function () {
    // Success returned from executeQueryAsync
    //Now we need to get the new ID and add that to ref.
    var newID
    newID = ghItem.get_id();
    //alert(newID);
    ghList = web.get_lists().getByTitle('Listname');
    ghNewItem = ghList.getItemById(newID);
    context.load(ghNewItem);
    ghNewItem.set_item("FieldName", "REF" + newID);
    ghNewItem.update();
    context.load(ghNewItem);
    cancelNewRecord();
}),
function (sender, args) {// Failure returned from executeQueryAsync
    alert("Failure " + args.get_message());
}

}

The Record is being added an I am getting the newID using ghItem.get_id() but i cant seem to then update the same record with the new REF using ghNewItem.set_item("FieldName", "REF" + newID);

Am I missing something?

Was it helpful?

Solution

  1. You don't need to get the object of the newly created item again as ghNewItem since it is already loaded as ghItem .
  2. You need another executeQueryAsync call at the end

    function saveDraftGHrecord() {
    ghList = web.get_lists().getByTitle('Listname');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    ghItem = ghList.addItem(itemCreateInfo);
    ghItem.set_item("ReasonOther", $('#txtReasonOther').val());
    etc
    etc
    
    ghItem.update();
    context.load(ghList);
    
    context.executeQueryAsync(function () {
        // Success returned from executeQueryAsync
        //Now we need to get the new ID and add that to ref.
        var newID
        newID = ghItem.get_id();
    
        ghItem.set_item("FieldName", "REF" + newID);
        ghItem.update();
        context.load(ghItem);
        context.executeQueryAsync(function () {},function () {})
        cancelNewRecord();
    },
    function (sender, args) {// Failure returned from executeQueryAsync
        alert("Failure " + args.get_message());
    });
    
    }
    

OTHER TIPS

After setting the REF field content and instructing the content to load it, you need to call the executeQueryAsync method again.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top