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.
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?
Well yes, it is the id of the previous insert action.
Here is my sequence of actions:
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.
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.
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.
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
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?
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?