Help

Updating record Linked Field with a string array

Topic Labels: Scripting extentions
Solved
Jump to Solution
2159 3
cancel
Showing results for 
Search instead for 
Did you mean: 

Good morning everyone,

I can’t seem to figure out the correct syntax for updating a Linked Field within a record.

My test base is simple, two tables, a name field in each, and a linked record between the two.

Table First

image

Table Second

image

Example of my simple script below. It’s odd that in the Airtable GUI users may paste text straight into the “Second” link and it creates the items in the Second table. But scripting requires the object/array to be formatted - which I’d appreciate a hand on. :pray:t2:

let table = base.getTable("First");
let query = await table.selectRecordsAsync({fields: []});
let recordId = query.records[0].id;


//Updating the Name works as expected
await table.updateRecordAsync(recordId, {
    "Name": "Example Record",
})

const data = ["Item 1", "Item 2"];

//Needing a hand to stamp "Name" items into the Linked Record.
await table.updateRecordAsync(recordId, {
    "Second" : data
})

output.text("Updated a record!");
1 Solution

Accepted Solutions

Heya Hendrik,

Using downtime that I’ve had today, I’ve figured out the correct syntax - and it’s simply awesome to see if finally working. Below is a screenshot for future me to refer to, along with anyone else looking for a straight up working answer to automating Linked Field record creation.

The below works with an AUTOMATION script, upon a new form record being created, that then spins off several sub tasks. Although there’s more work to be done here, below is the start that most first time coders will be stuck on.

//This script executes upon a new Project record being created, where users select sub-tasks from a dictionary that need to be carried out within that Project.

let inputConfig = input.config();
let table = base.getTable("Requested Task"); //This is the sub-task table.

//This loop picks the sub-tasks requested out of the new Project record.
for (let item of inputConfig._items_requested_array_data) {
     //Formats the task specific to the parent project.
    let my_new_record = `${item} - ${inputConfig._new_form_entry_name}`;    let recordId = await table.createRecordAsync({
        "Task ID": my_new_record,
        "Proj Record": inputConfig._new_form_entry_id, //Simple example writing record to a string, but this won't work for Linked Field. 
        "Project ID"   : [{id:`${inputConfig._new_form_entry_id}`}] //THIS is how to write it. 
    });
    console.info(`Created: [${recordId}] as ["${my_new_record}"]`);
};

Note; the above could be optimised, but works for the time being and should get users the simple understanding they’ll need to tackle this over and over.

The key here, is understanding the difference between writing a string into a text field, vs the syntax required to write the same record info into a Linked Field;

        "Proj Record": inputConfig._new_form_entry_id,
        "Project ID"   : [{id:`${inputConfig._new_form_entry_id}`}]

Some happy snaps of my base - happy coding everyone. :partying_face:

The Project table

New Form entries appear here.
image

The Task List creation

As each task is created for a new project, it’s branded with both the Task Dictionary and the Project that called for the task, and importantly it’s now linked to the parent project

image

The Task Dictionary

Tasks selection available for users to select from via the form.
image

Some may notice that the “Requested Items” column still hasn’t been linked - working on that now - anyone following, see if you can figure it out too, should hopefully be easier now. :partying_face:

See Solution in Thread

3 Replies 3

I THINK must be done like this.

let updates = First.map(x => ({
    id: x,
    fields: {
        "First Link": {name: statusToUpdateTo}
    }
}))

Heya Hendrik,

Using downtime that I’ve had today, I’ve figured out the correct syntax - and it’s simply awesome to see if finally working. Below is a screenshot for future me to refer to, along with anyone else looking for a straight up working answer to automating Linked Field record creation.

The below works with an AUTOMATION script, upon a new form record being created, that then spins off several sub tasks. Although there’s more work to be done here, below is the start that most first time coders will be stuck on.

//This script executes upon a new Project record being created, where users select sub-tasks from a dictionary that need to be carried out within that Project.

let inputConfig = input.config();
let table = base.getTable("Requested Task"); //This is the sub-task table.

//This loop picks the sub-tasks requested out of the new Project record.
for (let item of inputConfig._items_requested_array_data) {
     //Formats the task specific to the parent project.
    let my_new_record = `${item} - ${inputConfig._new_form_entry_name}`;    let recordId = await table.createRecordAsync({
        "Task ID": my_new_record,
        "Proj Record": inputConfig._new_form_entry_id, //Simple example writing record to a string, but this won't work for Linked Field. 
        "Project ID"   : [{id:`${inputConfig._new_form_entry_id}`}] //THIS is how to write it. 
    });
    console.info(`Created: [${recordId}] as ["${my_new_record}"]`);
};

Note; the above could be optimised, but works for the time being and should get users the simple understanding they’ll need to tackle this over and over.

The key here, is understanding the difference between writing a string into a text field, vs the syntax required to write the same record info into a Linked Field;

        "Proj Record": inputConfig._new_form_entry_id,
        "Project ID"   : [{id:`${inputConfig._new_form_entry_id}`}]

Some happy snaps of my base - happy coding everyone. :partying_face:

The Project table

New Form entries appear here.
image

The Task List creation

As each task is created for a new project, it’s branded with both the Task Dictionary and the Project that called for the task, and importantly it’s now linked to the parent project

image

The Task Dictionary

Tasks selection available for users to select from via the form.
image

Some may notice that the “Requested Items” column still hasn’t been linked - working on that now - anyone following, see if you can figure it out too, should hopefully be easier now. :partying_face:

FA
4 - Data Explorer
4 - Data Explorer

Life saver this. Thanks a lot