Help

Launched: get the active table/view in the scripting block!

cancel
Showing results for 
Search instead for 
Did you mean: 
Tim_Deng
Airtable Employee
Airtable Employee

Hi y’all!

The Cursor API is now accessible within the scripting block. You can use it to access the currently active table ID and view ID. This is particularly useful for removing the need for your users to manually select tables and views while running a script.

Learn more about how to use it in our scripting block API documentation.

Screen Recording 2020-08-06 at 04.06 PM

Hope you all enjoy the new feature; happy scripting!

8 Comments
Leo_Ahrens
4 - Data Explorer
4 - Data Explorer

I can’t seem to get cursor. activeTableId to work while scripting in an automation. Has this been applied there yet?

Would like to pull the name of the table from a record when an automation is triggered on that record. Not sure if active table is even the answer for that but it’s my best bet so far.

Thanks
Leo

kuovonne
18 - Pluto
18 - Pluto

There is no active table in an automation script. The concept only applies in the case when a user is looking at the user interface.

You can the the table ID for the triggering record from the record url. You can get the record url as an input variable. From that, you can get the table object, which will give you the table name. Of course, once you have the table object, you probably don’t need the table name.

Karlstens
11 - Venus
11 - Venus

Hey, this is neat! Thanks for sharing @Tim_Deng, it works well. You now have me thinking of all the ways that I need to start using this. :grinning_face_with_big_eyes:

:coffee:

Alexey_Gusev
13 - Mars
13 - Mars

I think, that example should be here.
script to order lookup values in cell according to their some ‘rating’ in other table, when you know just lookup and rating field names. designed to run from button. indeed, that means - to order links in link field, as lookup values follow their order.
also, @Sefik_Serengil may find something useful for his post

if (cursor.activeTableId){
const table=base.getTable(cursor.activeTableId)
const record = await input.recordAsync('', table);
const lookup=table.getField('Lookup_field_to_order')
const target=table.getField(lookup.options?.recordLinkFieldId)
const linkTOids=record?.getCellValue(target).map(lnk=>lnk.id)
const table2=base.getTable(target.options?.linkedTableId)
const query=await table2.selectRecordsAsync({fields:[],
sorts:[{field:'some_OrderField',direction:'desc'}],recordIds:linkTOids})
const idsTOlink=query.recordIds.map(id=>({'id':id}))
if (record) await table.updateRecordAsync(record.id,{[target.name]:idsTOlink})}
Leo_Ahrens
4 - Data Explorer
4 - Data Explorer

Can you specify on what that line of code will look like? I’m having a hard time pulling anything other than the URL.

kuovonne
18 - Pluto
18 - Pluto

You need to parse the table ID from the url. The base ID is the part between the slashes that starts with app and is followed by 14 alphanumeric characters.

Leo_Ahrens
4 - Data Explorer
4 - Data Explorer

Oh ok, so I did this once here… I was just thinking maybe there was an easier way. Maybe my method is sloppy?

let inputConfig =input.config();

// FIND SOURCE TABLE

// Pull Table Id from URL
let globalRecordUrl = inputConfig.globalRecordUrl

//Find the ID in the Url by searching /tb
let tableIdlink = globalRecordUrl.search("/tb");

//Find the end of the ID by adding 18
let linkEnd = tableIdlink + 18

//Pull out the ID by defining start and end point to trim URL string
let sourceTableId = globalRecordUrl.substring(tableIdlink +1, linkEnd);

// Defining Table by inserting found ID
let sourceTable = base.getTable(sourceTableId)
let sourceTableName = sourceTable.name

output.set ('Source Table Name', sourceTableName)
kuovonne
18 - Pluto
18 - Pluto

There are many ways to do things in code. I’ve experimenting with getting the table id using regular expressions, and with using split. Currently my favorite method is regular expressions, but some people are not comfortable with regular expressions.

Ultimately you should use a method that you understand and that makes sense to you. And do not be afraid to refactor you code when you feel a different method makes more sense.