Hi all,
I’ve been using Airtable for a few years and have finally decided to try my hand at writing a few scripts to streamline our workflow. I’ve coded a fair amount before but am new to JavaScript, so please excuse my ignorance.
My current project is writing a script that will be called from a button field. This script will make a request to a taxonomic database, retrieve classification data for a single creature, and populate a series of self-linked columns within the table. I have a working script that gathers the data and reformats, but I’m stumbling across the last step of assigning data to the linked fields. Throws this error after the last line:
Can’t set cell values: invalid cell value for field ‘kingdom’.
Cell value has invalid format: must be an array.
Linked records field value must be an array of objects with property ‘id’ corresponding to linked record id.
I understand that the issue has to do with needing the data to be formatted as an array for linked fields, but I’m afraid I don’t understand JavaScript well enough to fully grasp how to do that, especially for linked fields within the same table. Any advice would be appreciated!
Dummy code:
const API_KEY = 'API Key'
let table = base.getTable("Table")
let record = await input.recordAsync('Pick a record', table)
let url = record.getCellValue("url_field")
const response = await fetch(url);
const data = await response.json();
function unnestWormResponse(aphia, accumulator = {}) {
if (!aphia) return;
accumulator
unnestWormResponse(aphia.child, accumulator);
return accumulator;
}
const output_data = unnestWormResponse(data);
var empty = {
kingdom: "",
phylum: "",
class: "",
order: "",
family: "",
genus: "",
species: ""
}
empty.kingdom = output_data.Kingdom
empty.phylum = output_data.Phylum
empty.class = output_data.Class
empty.order = output_data.Order
empty.family = output_data.Family
empty.genus = output_data.Genus
empty.species = output_data.Species
await table.updateRecordAsync(record, {'kingdom': empty.kingdom})