Help

Button API Script - how to populate self-linked fields?

Topic Labels: Scripting extentions
Solved
Jump to Solution
1290 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Tyrel_Froese1
4 - Data Explorer
4 - Data Explorer

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[aphia.rank] = aphia.scientificname;
  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})
1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Linked record fields need to be an array of objects, where each object has an id that is the internal record id of the record to be linked to. That is quite a mouthful. It is easier to see if you do a read of an existing linked record field that has a value. The read format and write format are the same for linked record fields in scripting.

This also means that you cannot create the link using only info fetched from the API. You will need to find the record that matches info from the API, then use the record id if that record to make the link.

See Solution in Thread

3 Replies 3
kuovonne
18 - Pluto
18 - Pluto

Linked record fields need to be an array of objects, where each object has an id that is the internal record id of the record to be linked to. That is quite a mouthful. It is easier to see if you do a read of an existing linked record field that has a value. The read format and write format are the same for linked record fields in scripting.

This also means that you cannot create the link using only info fetched from the API. You will need to find the record that matches info from the API, then use the record id if that record to make the link.

Ok, thanks for the information @kuovonne , I think I understand the idea at any rate. One issue is that the API will often have to create linked records that don’t exist yet. Is it even possible to do that with a script?

Scripting can create new records. You can link the new record when you create them or after you create them.