Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Modify a Link to Records field, from a script in a button

3772 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Adrienocode
5 - Automation Enthusiast
5 - Automation Enthusiast

I have a database with two tables “gifts” and “status”. I'd like to use a button to update the status automatically via a script. 

I searched the documentation and the forum, but I couldn't find any solution to adapt the script to my problem. Here's the code I've already added, and the options I've explored.

This is my code :

// Récupère la table Cadeaux
let tableCadeaux = base.getTable("🎁 Cadeaux");
console.log(tableCadeaux);

//Récupère le champo statuts de la table Cadeaux
let statut = tableCadeaux.getField(" Statuts");
console.log(statut);

// Récupère le record.id de la table Cadeaux
let queryResult = await tableCadeaux.selectRecordsAsync({fields: []});
let record = queryResult.records[0];
console.log(record.id);

// Récupère la table Statuts
let tableStatuts = base.getTable(" Statuts");
console.log(tableStatuts);

let newRecordIdToLink = '🎁 Réservé';
tableCadeaux.updateRecordAsync(record, {
    " Statuts": [
        record.getCellValue(" Statuts"),
        { id: newRecordIdToLink }
    ]
})

and the documentation I looked at : multipleRecordLinks
https://airtable.com/developers/scripting/api/cell_values#multiple-record-links

I'm getting an error that I don't understand, maybe you have an answer or the logic that I haven't understood yet.
Thank you in advance

5 Replies 5

Could you provide screenshots of the relevant fields and explain what you want the script to do?

Adrienocode
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello, thank you for your time TheTimeSavingCo.
The idea is simply to be able to change the status of a record when the button is clicked from an interface, because here the status is a “Link to Another record” and not a select.

I need to change the record I click on from an A status to a B status defined in the script.
But I'd also like to understand what I'm doing, and at the end of the script I can't see how to do it.

Ah thanks for the details!  If you could provide a screenshot of the specific fields involved I could try to help!

Adrienocode
5 - Automation Enthusiast
5 - Automation Enthusiast

Yes of course :

Adrienocode_0-1721907706383.png

Adrienocode_1-1721907732230.png

I only want to update the field ” Statuts” when I click on the button (which contains the script) in the table "🎁 Cadeaux"


Thanks a lot

 

Thanks!  Here's some code that I got working:

// Récupère la table Cadeaux
let tableCadeaux = base.getTable("🎁 Cadeaux");
console.log(tableCadeaux);

//Récupère le champo statuts de la table Cadeaux
let statut = tableCadeaux.getField(" Statuts");
console.log(statut);

// Récupère le record.id de la table Cadeaux
let queryResult = await tableCadeaux.selectRecordsAsync();
let record = queryResult.records[0];
console.log(record.id);

// Récupère la table Statuts
let tableStatuts = base.getTable(" Statuts");
console.log(tableStatuts);

let newRecordIdToLink = 'recz4E29hpLQtrkvy';
tableCadeaux.updateRecordAsync(record, {
    " Statuts": [
        ...record.getCellValue(" Statuts") || [],
        { id: newRecordIdToLink }
    ]
})

Screen Recording 2024-07-26 at 10.25.34 AM.gif

I changed `let newRecordIdToLink = '🎁 Réservé';` to use a record ID as that's what the linked field requires, and I also updated the code so that it could append the record to any existing values in the field