Skip to main content

How do you get the ID value of the last created item by the current user using jquery?

You can do this without the need of jquery and stick everything inside of a standard Calculated Control or even a Form Variable. I will show both ways.



 



Using the following list:





 



I have created a Classic Form inside of which I have two Calculated Controls:





 



In the topmost calculated control is the following formula:



(function(idResults){

return idResults[idResults.length - 1];

}(lookup({Common:ListName}, "Author", userProfileLookup({Common:CurrentUser}, "PreferredName"), "ID", true)))


 





(note: it's showing the red reference hyperlinks here because the above code values will convert into them. You can just copy / paste my code outright with no need to manually insert the references)



 



As for the Form Variable, I have created one using the same formula: 





 



and then inserted that into the Formula Field of the second Calculated Control:





 



Now when I run my form as MegaJerk, I see the ID value of 4, as it's the last Item that I created: 





 





 



If I run the form as SPTestUser, I will see 3:





 



------------------------------



Some Considerations



------------------------------



This code assumes that you are looking up items in the SAME list you're form is on. If you are not then you will need to replace the {Common:ListName} with the actual name of your list, like:



lookup("List Name", "Author", userProfileLookup({Common:CurrentUser}, "PreferredName"), "ID", true)

 



If the List that you're targeting is on a different Site than the one the form resides on, then you'll have to add a path to the site in question on the left side of a pipe ("|") character before your List's name:



lookup("/sites/TopLevelSite/SubSite|List Name", "Author", userProfileLookup({Common:CurrentUser}, "PreferredName"), "ID", true)

 



This code will NOT update in real-time unless you reload the form! Lookup values are pulled during the initial form load and are then cached. If a user were to open two forms at once, they would BOTH pull in the same value initially. In turn they could submit one form, and then the next, and if any data was being determined by the Last Created ID Value, you would see a duplication in the results. If a condition like that could result in something blowing up, consider accounting for it now.



 



That is all I can think of at the moment. I hope that this helps you. 



 



 


Great! Thank you very much for your help.


Reply