Look up AirTable reference and return most recent item

Hi,

[Note: I initially included 6 screenshots to show what I was talking about, but I’m limited to 3 as a new forum poster. Hopefully I can report in replies later…]

I have been making a game with Thunkable X in which people are able to claim and capture hexes. I have set up an AirTable to record a list of hexes and who owns them, with columns called Hex ID (for the selected sprite), Row (for the internal _id number assigned by AirTable) and Owner (for the currently controlling player).

(Image 1 - the AirTable columns)

I used a tutorial to find the hidden AirTable Row ID reference and record that as a column to register each hex when I select it…

(Image 2 - app showing code for finding and recording AirTable row ID in a column in AirTable)
(This works fine)

When I select a hex, I can get it to display the current hex (first string of numbers/letters in screenshot below), then use this to look up the Row ID in AirTable to display it (second string in screenshot below, all starting with ‘rec’):

(Image 3 - screenshot of app preview, showing two Labels: 1 - Thunkable-assigned ID for current hex sprite / 2 - Row ID of AirTable row with the hex sprite ID listed in it)

With the most recent Row ID for the AirTable, I can then look up other columns and cells in the AirTable, using this as a reference (e.g. to look up the current owner of the hex).

If it is the first time the hex is selected, it displays the Row ID no problem and I can carry on with the program, but…

The problem is if the hex has been clicked previously (e.g. claimed by somebody else or logged earlier), that hex is referred to on more than one row. In this case, the Row ID call from AirTable lists every Row ID that the current hex has ever been referred to in, returning them as a long, comma-separated list, all starting with ‘rec’ (sorry that you can only see a couple of the screenshot, but there are 5 showing on my test phone):

image

The problem with this is that I can’t then use this to look anything up. For the game, I do want to be able keep and see previous listings for the hex (to see changes of ownership etc.), but I just want to be able to call up the most recent Row ID only so that I can then use this to find other cells.

image
(The same hex is in this list 5 times, but I only want the most recent ID)

I have found numerous helpful YouTube and Thunkable Community posts that have helped me to get this far, but I can’t seem to get the last roadblock out of the way, since most of these are happy to return a list, when I only want the last value.

This is my code as it stands:


This code works to return the Row ID for the currently selected hex, but unfortunately returns the ID of every row for that hex as a comma-separated list.

When I try to use Lists commands to find the Last in the list, it seems to break it and either return nothing or a single-digit number (the same number for whichever hex I select), so I think I’m doing that wrong.

Desired outcome:

  • When Function ‘Get Current Row ID’ is run, set variable TempHexRowID (and/or label Label_HexRowID_Hidden) to the most recent Row ID.
  • Return the most recent Row ID for the selected hex as the outcome for the Function called Get Current Row ID

Any help appreciated! I am so impressed at this community, having done a lot of reading on it recently, as well as back in the day from previous projects (I cam over from App Inventor when Thunkable was new). I hope this makes sense and that somebody is able to help!

1 Like

Have you used the unique row id feature offered from airtable?

1 Like

Thank you for coming back to me so quickly! Can you explain, please?

The difficulty I have found so far is that for AirTable, all of the Get commands need a Row ID, except for GetAllRows, but this gets all of them and I can’t specify a particular one. I have used the ‘For each item in j list’, but that doesn’t seem to work when I create a list and then try to ‘Find first occurence’, because it lists them all as one entry in the list, so the ‘first occurrence’ is all of them. I’m sure there’s a way to do it right though!

P.S. - I’ve used a command to find the Row ID and set this as a column, so every row has a unique ID that I can see in the ‘Row’ column (in my screenshot), but I can’t work out how to get back to that when someone selects a hex.


(Saving the Row ID as a value in the ‘Row’ column)

My desired workflow:

  1. Player clicks to select hex.
  2. Game looks up hex ID in AirTable using ActiveHex reference.
  3. AirTable returns list of all Row IDs mentioning the active hex.
  4. Last (most recent) value from that list is extracted to give the ‘current’ Row ID for that hex.
  5. This value is returned, allowing other properties (current Owner etc.) to be read and/or modified.

Basically I just want a way to read the most recent Row ID.

1 Like

Will it be possible to add another column (field) to your table as a Timestamp?

If yes, then you would be able to get the latest timestamp for the selected “hex” which should give the latest used “hex”.

Another way to get the rowID is the use the component “list all values” in the data source drawer. The component requires:

-The source which is your Airtable.
-The table which is the table in your Airtable.
-The column in the table which you can select any column or select “ID” (a reserved internal column name) which is the rowID you need. This is the one you are extracting from “_id” when you create the record. This saves you the duplication effort.

Happy Thunking

1 Like

Thank you!

I have added a timestamp column, but I don’t know how I would avoid the same problem (it would return all timestamps for the selected Hex ID and I only want the most recent one), but I’ll give it a go.

The Data Source idea (with the Data Viewer? Hopefully I can set this to hidden and just extract the data) sounds like it might work, though. I will try this, see how I get on and let you know…

Thanks again!

1 Like

Hmmm… I can’t seem to find the ‘list all values’ for the Data Viewer. I’ve linked the API and can get it to display the data when I tell it to display the data manually, but can’t work out how to call to it to look up the right data when I click a hex sprite and set the ActiveHex variable.

I don’t want to set the Data Viewer as visible (I want players to click a hex on the hex map to select it), so I don’t know how to get the selected hex into the viewer to display the data.

This set of blocks sort of works, but returns all row values instead of just the last one:

Sorry to make it more complicated!

1 Like

This is the component

It is in the block editor in the Data Source drawer.

The block of codes you showed will be very helpful with this component because this component will give you a list of all rowIDs. You can then loop through the rowIDs to read the table and only get the records matching your “hex” and save them to a temporary list along with the timestamp. Sort the resulting list and read the first item which has the latest timestamp.

1 Like

Thank you for this, which is really helpful. I’ll give it a try!

Thanks again!

1 Like

Sorry, I just seem to be getting in a jumble, as I can’t work out where to place these blocks to make it work. I managed to get it to display the Created column (time stamp) as well as the Row column (_id), and I think I managed to sort it, but I couldn’t then get the 1st item (even when I told it to).

I couldn’t work out where to put in the block you mentioned: image

Does it replace one of the blocks in my existing setup, or add to it, and if so where?

Really sorry to seem so confused. I can set up a call to go through it live if possible, but sadly I think I’m going to struggle without step by step, but I know that this is very difficult to do without the project shared to look at at the same time.

Sorry, I really appreciate your help and the time you have given me already.

1 Like

Let me create a sample app and share it so you can see what I mean. I’m also ready to take your call if we set up a time but without the app shared it will not be fruitful.

Will do the sample app tomorrow. It’s 3:30am where I am now.

1 Like