How Do I Get The ID of Last Created Item?

I want to query the database for the ID of the last created contact… so the highest ID value.

I have a server action with a Query Type “Single” as shown below, but whichever way around I order the sort, it is always returning the lowest ID value and not the highest.

What am I doing wrong? :thinking:

Hello Antony,
Are you trying to access the last inserted ID immediately after the insert is done or are you just trying to get the highest ID from a table in the database?

Look at the previous insert action and use the ‘identity’ field from that.

@sitestreet this will only work in a server action doing an insert.
If he’s querying a random table without an insert this method won’t really work.

1 Like

Well yes, it is the id of the previous insert action.

Here is my sequence of actions:

  1. I have a form, and on submit it will either do a database insert or database update, depending on whether the value of the $_POST.id is 0 or greater than 0.

  2. On the form’s submit success I run a flow, passing in the value of the form’s hidden input id. At this stage, the value is still 0 if I had just done a database insert.

  • I need the value of the newly inserted id to add to a data store insert action.

I’m not sure I know how to “use the identity field from the insert”… if that is possible, then great, but I can’t see how to access that from the menu system.

Thanks for your help on a lockdown Sunday! :slight_smile:

Antony.

Hi Antony

I would do all the work in the server action. Use a GET variable to hold the ID and put in a condition - if it’s 0 then do an insert, if it’s > 0 do an update of the ID of that variable.

Does that help?

1 Like

I’d also follow what @sitestreet suggests.
Just do everything in in one Server Action.

The inserted id can be found in the data picker in the steps added after the insert step…

1 Like

Thanks for your help folks!

At the moment, my server action is a POST, triggered off the form submit.

To be able to receive the ID back into the design, what do I do?

Put in some extra step after the database insert? What would that be?

Or do I have to convert the whole server action to be based on get variables?

Sorry, I’ve no experience of this beyond the simple things I have learned in Wappler so far! :man_shrugging:

SA so far is shown below…

Antony.

If you enable the insert record output option it will return the inserted identity when you submit the form.

1 Like

After the insert stage you can see the identity via the data picker. it will be visible in server Connect and App connect if marked for output.

image

1 Like

Well lo and behold, so it is!

Thanks folks!

But I have realised this is not the whole story…

I need to do the same thing for creating an event, and that is created in one massive stored procedure as a gazillion other things are created at the same time.

So I still need that “Get me the last ID” server action to work.

Why am I always getting the lowest id and not the highest, no matter how I set the sort direction?

The id returned will always be the LAST id assigned in an insert action. The affected (think that really should be effected) is the number of rows inserted.

Using a query to find the highest number and assuming that is the last id inserted is a really bad thing to do as it makes the assumption that no other inserts have happened since. In a high volume environment that is quite possible

2 Likes

I just tested this with a single query and sorted by ID column, DESC. It works as expected.
What column are you sorting exactly? What type?

Thanks for trying that out Teodor!

I’m ordering by an id column, so defined like this:

CREATE TABLE IF NOT EXISTS contacts (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL);

Well if this is the way I should be doing it:

xx

Then… I don’t know… maybe it will work tomorrow… maybe it’s having a rest day today… :slight_smile:

Are you sure the filter you are using returns more than one record… (user identity) ?
What if you remove it for testing purposes, is it still not sorting the data properly?

Have you tried using mysql_insert_id() function inside your stored procedure to get the last returned id?

Good question. Am using Single Record mode as don’t want to return more data than needed.

Do I need Multiple Record mode then take element [0]?

Yes, I could do that and return its id, that is true!

Well it should sort the data anyway.
What I am asking is ARE YOU SURE that it returns more than one record when you filter with that user id? If it’s only one - how would you expect it to sort it?

or a paged recordset, offset 0 limit 1