Help

How to get a record ID of the latest entry?

Topic Labels: API
Solved
Jump to Solution
8649 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Neetzie
4 - Data Explorer
4 - Data Explorer

I only have 1 table and I need the most recently added record.

I tried this code and even tried maxRecords so loop through each id.

base(‘Intention’).select({
filterByFormula: ‘{id} = “id”’
}).eachPage(function page(records, fetchNextPage) {
records.forEach(function(record) {
console.log(‘Retrieved’, record.get(‘id’));

Any tips on how I should approach this?

1 Solution

Accepted Solutions
Bill_French
17 - Neptune
17 - Neptune

I think @Neetzie is using Airtable.js, an SDK for the Airtable API.

I tend to avoid the SDK (old-school API hack is my thang). But, I do know that despite the SDK providing a filter method, it still must enumerate all records to find the last one and its record ID.

Given the current API, there are no methods (as far as I know) to get the last record ID without traversing all pages (collections of 100 records) and all records. Furthermore, there is presently no API method to dump a list of records constrained in a fashion to include only certain fields (which would be a very lightweight request that didn’t involve pagination).

My Approach…

If I want to get the most recent record ID in a table, I…

  1. Request the entire table
  2. Iterate across the records to construct a 2D array containing only the record IDs and created date
  3. Sort the array [descending] on the created date

The last known record ID will be myArray[0].recordID. I believe this is pretty much what your SDK code is attempting to do, but I can’t see enough of it to assess why it isn’t working as expected.

If there’s a more elegant (and efficient) way to do this, I would love to see one.

See Solution in Thread

4 Replies 4
AlliAlosa
10 - Mercury
10 - Mercury

Where are you entering this code?

Are you totally against adding a new table? You could make use of a summary table, where each record on your main table is linked to one record on the summary table. When I use this approach, I like to group my main table by that linked record field to ensure that new records are always linked to the summary table as well.

If you choose to add the summary table; the following steps will give you the record ID for the most recent record.

Add a CREATED_TIME() field to your main table. Let’s say it’s called {Date Created}.

Add a rollup field to the summary table, pointed at {Date Created}, and use the formula function:

MAX(values)

This will give you the date of the most recently created record. Let’s say the above rollup field is called {Most Recent Record}. Add a lookup field to your main table, and pull {Most Recent Record} to your main table.

Next, add a formula field to your main table, with the formula…

IF(IS_SAME({Most Recent Record}, {Date Created}), RECORD_ID())

Say this field is called {Most Recent Record ID}. Finally, use a rollup on your summary table, pointed at {Most Recent Record ID}, with the formula function:

ARRAYCOMPACT(values)

This should give you the most recent Record ID on the summary table. You could add an extra step of pulling that Record ID back to your main table with a lookup field if you only want to work with one table in whatever integration you are using. :slightly_smiling_face:

Actually, depending on your use case, you might not need the final couple steps. {Most Recent Record ID} should only leave one record on your main table populated with a Record ID. Depending on what app/integration you’re using to loop through the fields, you might be able to narrow down your search with a boolean for that field.

Bill_French
17 - Neptune
17 - Neptune

I think @Neetzie is using Airtable.js, an SDK for the Airtable API.

I tend to avoid the SDK (old-school API hack is my thang). But, I do know that despite the SDK providing a filter method, it still must enumerate all records to find the last one and its record ID.

Given the current API, there are no methods (as far as I know) to get the last record ID without traversing all pages (collections of 100 records) and all records. Furthermore, there is presently no API method to dump a list of records constrained in a fashion to include only certain fields (which would be a very lightweight request that didn’t involve pagination).

My Approach…

If I want to get the most recent record ID in a table, I…

  1. Request the entire table
  2. Iterate across the records to construct a 2D array containing only the record IDs and created date
  3. Sort the array [descending] on the created date

The last known record ID will be myArray[0].recordID. I believe this is pretty much what your SDK code is attempting to do, but I can’t see enough of it to assess why it isn’t working as expected.

If there’s a more elegant (and efficient) way to do this, I would love to see one.

Neetzie
4 - Data Explorer
4 - Data Explorer

@Bill.French @AlliAlosa Thank you both for the pro tips, I’m learning a lot from your suggestions. @Bill.French, you’re right–my SDK code was attempting just that. It did the trick!