Get Last Item ID not working

Jun 24, 2011 at 1:33 PM

I am using the following code and i am noticing that sometimes the lastId value is not the correct value. For example, my list shows i added 3 items with ids fo 1619, 1620, and 1621, but the alert showed that the last id was 1620. Am I misunderstanging the purpose of this function?



 for (i=0;i < count;i++)
   thisID = items[i].id;
 // Get the selected items that have Required Reviews
  //alert ("thisID -"+ thisID + " i =" + i + " count="+count);   
   operation: "GetListItems",
   async: false,
   listName: "Authorities",
   CAMLViewFields:"<ViewFields><FieldRef Name='Category' /><FieldRef Name='Activity' /><FieldRef Name='Role' /><FieldRef Name='Responsibility' /></ViewFields>",
   CAMLQuery:"<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+ thisID +"</Value></Eq></Where></Query>",
   completefunc: function (xData, Status){
   //alert ("CAMLQuery" + CAMLQuery);
   // For the retreived item (you might want to grab more than one at a time)
   //create new item in Delegations list for each selected activity
    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
     strActivity= $(this).attr("ows_Activity");
     respCode =$(this).attr("ows_Responsibility").split(";#")[0];
     //alert ("RespCode - " + respCode );
     //alert ("RespDescr - " + respDescr);
     //respFull = respDescr + ' - '+ activityDescr; //used for email body
     //alert ("respFull - " + respFull);
      operation: "UpdateListItems",
      async: false,
      listName: "Delegations",
      //batch: The batch command to tell UpdateListItems what to do
      batchCmd: "New",
      valuepairs: [["Activity", strActivity],["EndDate", endDate],["StartDate",startDate], ["Delegate", delegateAccount], ["Delegator",delegatorAccount],["Responsibility",respCode],["ItemInBatch",i+1],["TotalInBatch",count],["Client",client],["Sector",sector],["ContractNumber",contractNumber], ["JobNumber",jobNumber],["DelegatorRole",role],["DelegatorTitle",delegatorTitle],["BusinessUnit",businessUnit]],
      completefunc: function(xData, Status) {
       //alert("completed title-" + strTitle);

var lastId=$().SPServices.SPGetLastItemId({
alert("lastId " +lastId);

Jun 24, 2011 at 1:54 PM


The function should be returning the last item ID in the specified list created by the current user (unless you specify a user).

If you are seeing behaviour different than that, the it would be helpful to see a test case. I know that would be hard to do, but maybe a video?


Jun 24, 2011 at 6:13 PM

Marc, it turns out i had the lastId variable initiated inside of a function and it did not have the proper scope. I made it global and it seems to be working correctly now. Thanks for you help.

FYI, just heard your interview on the Consultants Chronicle podcast. I enjoyed listening to what you had to share.


Jun 24, 2011 at 7:32 PM

Oops. Sorry I didn't spot that.

Glad you found the interview enjoyable!