Skip to content

Instantly share code, notes, and snippets.

@kopischke
Last active September 1, 2024 00:16
Show Gist options
  • Save kopischke/35b0511d86504e466e50f08e19404c84 to your computer and use it in GitHub Desktop.
Save kopischke/35b0511d86504e466e50f08e19404c84 to your computer and use it in GitHub Desktop.
Code snippets for Script actions in Airtable Automations
// Utility functions for Airtable Automations
/**
* @param {string} url
* @param {Base} [forBase=base]
*/
function getTableFromURL(url, forBase) {
let root = forBase ? forBase : base
let found = url.match(/^https?:\/\/airtable.com\/(?<tableID>[^\/]+)/)
return root.getTable(found.groups.tableID)
}
/**
* @param {string} id
* @param {Table} inTable
*/
async function getRecordByID(id, inTable) {
let records = await inTable.selectRecordsAsync()
return records.getRecord(id)
}
// Code snippets for Airtable Automations
// Update record fields, by default only when they have no value yet
const update = {}
const fields = [
{name: '__name__', value: '__value__'},
{name: '__name__', value: '__value__', always: true}
]
fields.forEach(f => {
if (f.always || !record.getCellValue(f.name)) update[f.name] = f.value
})
if (Object.keys(update).length) await table.updateRecordAsync(record.id, update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment