Why does my oListItem.get_id() return -1 even though executeQueryAsync succeeded on creating of list item?

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

  •  08-02-2021
  •  | 
  •  

Question

I created a function that adds an oListItem with some lookup columns as shown

    function on_click(){
        console.log("creating new assessment")
        var siteUrl = _spPageContextInfo.webAbsoluteUrl;
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('AssessmentAnswersList'); 

        var itemCreateInfo = new SP.ListItemCreationInformation();
        var oListItem = oList.addItem(itemCreateInfo);

        var lookupCourse = new SP.FieldLookupValue();
        var lookupTrainee = new SP.FieldLookupValue();
        var lookupAssessment = new SP.FieldLookupValue();
        lookupCourse.set_lookupId(48); //selected ddl value
        lookupTrainee.set_lookupId(1132); //selected ddl value
        lookupAssessment.set_lookupId(21); //selected ddl value
        oListItem.set_item('Title', "Course Assessment Results"); //input val()
        oListItem.set_item('CourseName', lookupCourse);
        oListItem.set_item('AnswerBy', lookupTrainee);
        oListItem.set_item('AssessmentTaken', lookupAssessment);
        oListItem.set_item('TotalScore', 0); //input val()
        oListItem.set_item('NoOfAttempts', 1); //input val()

        console.log("Saving assessments...")
        //console.log(oListItem);   
        oListItem.update();

        clientContext.load(oListItem);

        clientContext.executeQueryAsync(Function.createDelegate(onSaveSucceeded(oListItem)), Function.createDelegate(onQueryFailed));
    }

    function onSaveSucceeded(item){
        var id = item.get_id();

        alert("Saved! The ID is: " + id + ' and ' + item.get_item('TotalScore'));
    }
    function onQueryFailed(){
        alert("Error");
    }

For some reason, onSaveSucceeded function's item.get_id() always returns -1, but when i check the list, the item has been properly created. How do I rectify this? I need the ID to do further processing and I cannot figure out why I am not being returned the correct ID.

Was it helpful?

Solution 3

I found a solution to my problem. I apparently cannot use clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

I instead need to call it like this

clientContext.executeQueryAsync(function(){onSaveSucceeded()}, onQueryFailed);

This will return me the created ID that I need to do further processing.

I'm not sure why I cannot use Function.createDelegate when doing an insert, any insights on this would be greatly appreciated. But if anyone else is ever facing the same situation as me, I hope this solution helps you too.

OTHER TIPS

You have already loaded oListItem in the clientContext at line:

clientContext.load(oListItem);

So,

Write your executeQueryAsync() as below:

clientContext.executeQueryAsync(Function.createDelegate(onSaveSucceeded), Function.createDelegate(onQueryFailed));

And then use below code to get the item ID:

function onSaveSucceeded() {
    alert("Saved! The ID is: " + oListItem.get_id() + ' and ' + oListItem.get_item('TotalScore'));
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top