Skip to main content
Solved

Can airtable chose a record randomly?

  • September 29, 2022
  • 3 replies
  • 108 views

Nathan_Ramey
Forum|alt.badge.img+9

Hello everybody! I was wondering is there a way to make it so that airtable and chose a record randomly?

Best answer by Vivid-Squid

Thank you! I was wondering could this be applied to a particular view? @Vivid-Squid


Sure try this. Make sure you are viewing the table and view, then run the script.

let curTable = cursor.activeTableId
let curView = cursor.activeViewId
let table = base.getTable(curTable)
let view = table.getView(curView)
let queryRecords = await view.selectRecordsAsync()
let recCount = queryRecords.records.length
let randomNumber = Math.floor(Math.random() * recCount);
let randRecord = queryRecords.records[randomNumber]
output.markdown(`
**Random Record**

Record ID: [${randRecord.id}](http://airtable.com/${base.id}/${table.id}/${randRecord.id})

Record Name: ${randRecord.name}
`)

3 replies

Forum|alt.badge.img+16
  • Inspiring
  • 529 replies
  • September 29, 2022

Hi @Nathan_Ramey,
If you add the scripting extension, paste this code in to replace what ever is there. It will return one random record from the table you are currently viewing.

let curTable = cursor.activeTableId
let table = base.getTable(curTable)
let queryRecords = await table.selectRecordsAsync()
let recCount = queryRecords.records.length
let randomNumber = Math.floor(Math.random() * recCount);
let randRecord = queryRecords.records[randomNumber]
output.markdown(`
**Random Record**

Record ID: [${randRecord.id}](http://airtable.com/${base.id}/${table.id}/${randRecord.id})

Record Name: ${randRecord.name}
`)


Nathan_Ramey
Forum|alt.badge.img+9
  • Author
  • Known Participant
  • 22 replies
  • September 29, 2022

Thank you! I was wondering could this be applied to a particular view? @Vivid-Squid


Forum|alt.badge.img+16
  • Inspiring
  • 529 replies
  • Answer
  • September 29, 2022

Thank you! I was wondering could this be applied to a particular view? @Vivid-Squid


Sure try this. Make sure you are viewing the table and view, then run the script.

let curTable = cursor.activeTableId
let curView = cursor.activeViewId
let table = base.getTable(curTable)
let view = table.getView(curView)
let queryRecords = await view.selectRecordsAsync()
let recCount = queryRecords.records.length
let randomNumber = Math.floor(Math.random() * recCount);
let randRecord = queryRecords.records[randomNumber]
output.markdown(`
**Random Record**

Record ID: [${randRecord.id}](http://airtable.com/${base.id}/${table.id}/${randRecord.id})

Record Name: ${randRecord.name}
`)