Skip to main content

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


Forum|alt.badge.img+2

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

TheTimeSavingCo
Forum|alt.badge.img+28

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


Forum|alt.badge.img+2

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.


TheTimeSavingCo
Forum|alt.badge.img+28
Adrienocode wrote:

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!


Forum|alt.badge.img+2

Yes of course :

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

 


TheTimeSavingCo
Forum|alt.badge.img+28
Adrienocode wrote:

Yes of course :

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 } ] })

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


Reply