Help

Scripting help - add related record to a field that already has related records

Topic Labels: Automations
Solved
Jump to Solution
3413 11
cancel
Showing results for 
Search instead for 
Did you mean: 
cori_parrish1
6 - Interface Innovator
6 - Interface Innovator

i am using a step in an automation that i’m hoping will allow me to add a related record to a field that already has related records in it. the prior steps in the automation are picking out which record i want the script done to.

i am brand new to scripting, so i’m guessing it’s something obvious. i got this from the documentation here: Airtable Scripting

table.updateRecordAsync(record, {
‘Which trainings?’: [
…record.getCellValue(‘Which trainings?’),
{ id: newTraining }]
});

when i test the step, it says the field “Which trainings?” can’t accept the provided value & when i ask it to show me what it got from the step above, it shows me an empty set.

the newTraining value is the record id of the related record i want to add to the field.

any help is appreciated!!

1 Solution

Accepted Solutions

@cori_parrish1

Use @JonathanBowen’s screenshot as a general guide above, but note that:

  • The new value has to go first, not last. Otherwise it will fail if there was nothing already in the field, due to the presence of a comma.

  • The record ID used for updating needs to be a single record ID, not a list of ID’s. Airtable can only update a single record, not multiple records.

  • While you can use record ID values for the linked record field, you can also use primary field values as well.

See Solution in Thread

11 Replies 11

I don’t think you need a script for this.

You’re just trying to append a new linked record to a linked record field that already has linked records in it?

What you can do is start by inserting the new linked record data into the field, then type a comma, and then insert the linked record field itself.

That will append the new linked record data to what is already in the field.

cori_parrish1
6 - Interface Innovator
6 - Interface Innovator

yes, i can do it by hand.

but i want the automation to do it for me, because i need to add it to all 100 people who came to the training. and i don’t want to create duplicate people, i want it to say Person X attended these 3 trainings.

and ultimately, i will want to use the input functions to let the staffer updating the overall attendance list choose which training they would like to add in this particular instance. but first i just want to get the script to work with the new training specified.

am i making sense?

My instructions above were for automating the process. That’s what you do in the “Update Record” step of the automation.

But I guess I’m not understanding what you’re trying to do.

cori_parrish1
6 - Interface Innovator
6 - Interface Innovator

it seems like it should be that simple. but when i tried it that way, it just overwrote the previous values with the new values. maybe i’m doing something wrong? i tried it with both the list of names & the list of record IDs in the prior field. thanks so much for your help!

Grunty
7 - App Architect
7 - App Architect

In my experience, that error message is thrown on either of three conditions:

  • the array you’re trying to (re)insert into the linked field is ‘null’ (not just ‘empty’, but hopelessly ‘null’). Linked fields abhor nulls.

  • the value you’re trying to append into the linked field is invalid, like null, empty, or any format other than ‘rec…’

  • the record ID you’re trying to append to the linked field already exists in it. Linked fields abhor duplicates.

So, prior to attempt the update make sure that:

  • current linked field value (retrieved by getCellValue) is not null. Linked fields are born null, until you initialize them.

  • the new record ID looks like the acceptable format: ‘rec…’

  • the new record ID does not currently exist in the linked field.

Then please tell us which of the three offences was causing the error.

@cori_parrish1 - you can definitely do this with an automation @ScottWorld says.

Screenshot 2022-10-22 at 15.08.20

This is an “update record” action. Don’t forget the comma in between the current list of linked records and the ID of the new record.

What you are doing here is a no code version of what you noted in your first post, i.e. using the spread operator to attached a new element to an array (in our case, a new ID to an existing array of IDs). So, you could also do this with a script - what you had looks correct, not sure why this isn’t working for you - but the no-code automaiton will work just as well.

@cori_parrish1

Use @JonathanBowen’s screenshot as a general guide above, but note that:

  • The new value has to go first, not last. Otherwise it will fail if there was nothing already in the field, due to the presence of a comma.

  • The record ID used for updating needs to be a single record ID, not a list of ID’s. Airtable can only update a single record, not multiple records.

  • While you can use record ID values for the linked record field, you can also use primary field values as well.

Yeah, that was a bit of a cheat on my part - I had a find records above this but only found one record (by design), so list worked OK for me, but as @ScottWorld says, the general pattern should be a single record id. :slightly_smiling_face:

Grunty
7 - App Architect
7 - App Architect

Hey @cori_parrish1,
Just a reminder that “Two and a half” :winking_face: experts have provided their advice to try and solve your automation/scripting trouble.
We’re holding our breath to know what the outcome was?