Retrieve record ID after insert

After inserting a record how do I get the new record ID back so I can use to load a table?
I am working on a job manager type page where the user will create new jobs and then add tasks, comments, and upload files for that particular job.
Currently, I display a list of jobs created for the user and sort the jobID as a variable that is used to populate three other tables.
What I want to happen is after a new job is created the new jobID is passed back to the variable and the other tables reloaded.
The only part I can’t figure out how to do is to get the jobID back after inserting the record. I have tried adding the insert to a serverconnect and then getting the identity after inserting but that did not work. I also try using the identity in the server action to query the database using the identity in the same server action as the insert. That also didn’t work.

This recent thread might be helpful. This from the docs should help too.

1 Like

This video should help

2 Likes

Try this post from George.

2 Likes

Thanks for the help, this was what I was looking for.

@Hyperbytes @George I have a multi-user scenario where there will be simultaneous inserts.
The MySQL docs say that last inserted id is accurate on per-connection basis.
So I just wanted to know if each call to server action creates a separate connection? I would assume so as we have to add the DB connection step in each server action.
OR, does the server create one single global connection and re-uses it on server action call?

Yes each server connect call makes its own db connection, so there will be no multi user issues. This is how it is supposed to work.

Within the server connection action the db connection can be reused for the different steps - but that is normal as it is all for the same action and user.

1 Like

Hi,
Is there a similar ‘How To’ that details using the new id and setting as a cookie or session? I have read the docs " Get Inserted Record ID" and several posts like this and “Inserted Record ID - how to pass between pages”, but can’t figure out how to get a new id set as a cookie.
Many thanks!

The process is exactly the same however once completed you set the cookie to the returned .identity in the success event

i.e.

Just check output for the insert query

image

The in App Connect ensure your cookie is defined in the cookie manager

image

The in the success event of your insert action set the cookie to the returned value

Then in the success dynamic event of the server action set the cookie to the value of the returned identity.

thank for that. looks like I had a couple of problems, but was also hung on something else maybe worth a mention.

  1. This form is on a card in a Masonry.
    image
  2. After selecting the databinding from within the dataconnectform
    image
  3. The value populated starts with masonry1. Wouldn’t seem like a problem, but the cookies was still not setting (cookie name is appears in browser, but the value is “undefined”).
    image
  4. So banging around trying stuff, I deleted the “masonry1” at the beginning of the value, and bingo…works. I re-created this scenario, and the same result; didn’t work using value created automatically, then did work with elimination of masonry1.
    Is it just me not getting things?
    Thank you again!
    image

No. it does look like putting a form inside a card inside a masonry repeat confused the GUI although it does seem a very strange thing to do, i don’t quite follow what you are trying to achieve by setting a cookie to the same value (identity) in multiple card scenarios? I guess someone like @George may understand why the GUI does that?

Thanks for your quick reply. I am using the cards (in masonry) as a cool kind of responsive menu for existing clients. On most cards, they serve as a simple link (ex. to a order history page, account details etc.), but this card initiates a new order, hence the insert and setting of cookie.
And yes, maybe @George or @Teodor could advise.
kind regards

But as a masonry is by definition a repeat, it will appear in all the cards?

Hi Brian,
To my surprise, masonry is repeating some attributes, like card size and layout (and animation), but the individual cards can have independent functions and attributes. In the quick (and plain) example below, the card on the left has a form embedded with hidden fields, while the one to the right is just a link. Also, things like button styling can be a la carte as well.
So, thanks to your help, this is working nicely as a responsive menu.
Hope that is what you mean. Best regards.
image