Dec 01, 2023 12:20 PM
Hello,
I'm working on an automation that, when triggered by a new record being created, runs a script. The script should take that record, parse a comma-delimited list, then create new records in a separate table. The new records are include the unique names, the count of each unique name, and a link back to the original record. I mostly have this sorted now thanks to some awesome older forum posts, but I'm having trouble understanding how to write new records.
The error I'm getting is on the last line of code where the records are being created:
I believe the issue is with how I'm assigning the records. I do not understand the correct syntax, so any help would be highly appreciated!
let inputConfig = input.config();
let fullnameList = inputConfig.fullnameList;
let sourceID = inputConfig.sourceID;
const uniquenames = [... new Set(fullnameList)]; //gets unique array elements see https://codeburst.io/javascript-array-distinct-5edc93501dc4
let nameTable = base.getTable('names');
let nameRecord = nameTable.records
let recordsToCreate = [];
for(let i=0; i<uniquenames.length; i++){ //check each unique name
let nameCount = 0;
let onename = uniquenames[i];
for(let j=0; j<fullnameList.length; j++){
let name_i = fullnameList[i];
if(onename == name_i){
nameCount += 1;
}
}
recordsToCreate.push(
{fields : {
"nameField" : onename,
"Part Count" : nameCount,
"Prints" : sourceID
}}
)
};
console.log(recordsToCreate);
await nameTable.createRecordsAsync(recordsToCreate);
Solved! Go to Solution.
Dec 01, 2023 04:26 PM - edited Dec 01, 2023 04:26 PM
Ah okay, yes, it needs to be the Airtable Record ID for the record you're linking to. Also, just to be sure, this should be just 1 single record ID.
One thing you could try is to hard-code the record ID to make sure the formatting works, where you replace recXXXXXXX below with your record ID in quotes
recordsToCreate.push(
{fields : {
"Geometry" : oneGeometry,
"Part Count" : partCount,
"Prints" : [{id: "recXXXXXXX"}]
}}
)
Dec 01, 2023 01:14 PM
Hey there! I've done some similar scripting things so I might be able to help.
To start, which field is `fldjndAnd48IHziyd`? Or what is the name? That'd help us dig into if the way you're writing to the field needs to be adjusted.
If you're not sure you can use the Tools > Manage Fields menu to find the field by field ID.
Dec 01, 2023 01:19 PM - edited Dec 01, 2023 01:19 PM
Howdy! Thanks for the tip, I didn't know how to find the field ID before. It looks like that one is the link to the original record.
The three fields I'm trying to write to are:
Name (single line text)
Part Count (number)
Prints (link to another record)
The naming is a little off because I'm trying to obscure and generalize this.
ID
Dec 01, 2023 01:26 PM
Gotcha okay thanks! In that case, the Scripting API will be our friend here for determining the format to write to the field giving the error.
In this case, for your Prints field, that would look something like this instead:
"Prints": [{id:sourceID}]
Because the write format for linked record fields requires an array of record IDs:
Array<{ id: string }>
or in easier to understand code:
[{ id: string }]
Hopefully that makes sense! Let me know if you have any questions!
Dec 01, 2023 03:53 PM
Thank you, that is very helpful. I got further along - I tested that the rest of the code works properly, but I'm still having trouble with the record linking bit. Here's what I have at the moment:
recordsToCreate.push(
{fields : {
"Geometry" : oneGeometry,
"Part Count" : partCount,
"Prints" : [{id: printID}]
}}
)
Here's what the console shows for the object being written
Dec 01, 2023 04:21 PM
2 more things:
1) Does the recordID need to be the actual recordID? I have tried both the actual record ID e.g. "recDtpSLcblG1fE4a" vs the name I'd type in to do the link, e.g. "ScottPrint2" and neither have worked, but I'll have to sort that out.
2) Here's the inline error message. I don't understand it well enough but I suspect this could be telling me exactly what's wrong:
Type '{ id: string; }' is missing the following properties from type 'readonly { id: string; }[]': length, concat, join, slice, and 18 more.
Dec 01, 2023 04:26 PM - edited Dec 01, 2023 04:26 PM
Ah okay, yes, it needs to be the Airtable Record ID for the record you're linking to. Also, just to be sure, this should be just 1 single record ID.
One thing you could try is to hard-code the record ID to make sure the formatting works, where you replace recXXXXXXX below with your record ID in quotes
recordsToCreate.push(
{fields : {
"Geometry" : oneGeometry,
"Part Count" : partCount,
"Prints" : [{id: "recXXXXXXX"}]
}}
)
Dec 01, 2023 04:30 PM
yay thank you so much! In my messing around with it I ended up testing the code without those [] brackets
recordsToCreate.push(
{fields : {
"Geometry" : oneGeometry,
"Part Count" : partCount,
"Prints" : [{id: recordID}]
}}
)
Dec 04, 2023 08:58 AM
happy to help 🙂