Current id - 1 filter to get the last inserted id
Application Type
Reactive

Hi, 

Is there a way to get the last inserted id to update its content? 

For example, I have the currentid and after creating the data for that. I will update the previousid to update some its content.

Thanks to the people who will answer.

Solution

Thanks to the answers. 

I did this using aggregate which has inputparamterid > tablesourceid as filter, maxrecord = 1, sort by desc then update data.

Then on other server action I have an aggregate which I filter it with the Current.list.TableId=createupdate_id(id from the current created data) then call the above action. 

Hello,


You can aggregate over the table with MaxRecords = 1 and sort by id DESC.


Regards,

Jorge Rodrigues

oh hello again haha! 

I did that already but I want to filter the data into the previous id. Like, I have the currentid = 47 which has been inserted into the database now, I want to filter the aggregate to idno = 46 to update some of its content.

Do you have everything in the same flow? Can't you make the aggregate before insertion?

Hello Matanong,

You can do it using a advance SQL. I have done to get the last maximum value and added 1 to get the next sequence. In your case you can do minus 1 after adding a record into the entity.

Thanks & Kind Regards,

Sachin

 


There is a warning that says "there is a mismatch between the number of columns selected and the output structure in sql1"

Please make sure that you have same number of columns into "select" statement and into the output structure. It looks like you have mismatch into the number of columns into your select statement and output structure. So for this reason you are getting this warning.

And in addition to the above comments, please refer the below link to know more about this.

https://success.outsystems.com/Documentation/11/Developing_an_Application/Use_Data/Query_Data/SQL_Queries?

Hi @Matanong ,

I think It´'s better to read the correct ID then calculatiing it based on Max number. Please find the example query for that.

Select Top 1 CustomerID from (SELECT TOP 2 CustomerID FROM Customers order by CustomerID desc) order by CustomerID asc ;

This query will always return the second last inserted record. 

Above query will return correct result even if any record is being deleted from the table and IDs are not in sequence.

Thanks,

Vani



Solution

Thanks to the answers. 

I did this using aggregate which has inputparamterid > tablesourceid as filter, maxrecord = 1, sort by desc then update data.

Then on other server action I have an aggregate which I filter it with the Current.list.TableId=createupdate_id(id from the current created data) then call the above action. 

I would not do it by getting the ID of the last record on server side logic. This does not work well in a multi user situation. If you have two users inserting records around the same time then yo could get the ID of the wrong record that way.

If you are inserting a record using the built in create or createorupdate actions then that action will return you the ID of the record that has just been inserted.

Ah yeah, that's what I mean on the createupdate_id(id from the current created data). I did not explain well. Thanks 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.