JHatza
Scholar

ID value of last created item

Jump to solution

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

0 Kudos
Reply
2 Replies
MegaJerk
Collaborator

Re: ID value of last created item

Jump to solution

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:

MegaJerk_7-1653630161859.png

 

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

MegaJerk_0-1653629765427.png

 

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)))

 

MegaJerk_1-1653629850438.png

(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: 

MegaJerk_2-1653629954279.png

 

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

MegaJerk_3-1653629994494.png

 

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

MegaJerk_5-1653630073645.png

 

MegaJerk_4-1653630055171.png

 

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

MegaJerk_6-1653630120897.png

 

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

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. 

 

 

0 Kudos
Reply
JHatza
Scholar

Re: ID value of last created item

Jump to solution

Great! Thank you very much for your help.

0 Kudos
Reply