May 01, 2022 08:21 AM
Hi there - How is it possible to add a list of IDs into a createrecordasync function?
For instance, we have:
id_array: [recK0FKsUDicD8Lar,recP4uFoqMq9qJquY,recudo4XILtrW5IYd]
const table_Output = base.getTable(“Output_Results”)
table_Output.createRecordsAsync([
{fields: {
Client: [{id: clientID}],
Symptom: [{id: {id_array}]}
}])
where we replace id_array with the actual array
Thanks!
May 01, 2022 09:11 AM
It looks like you are trying to set the field value of a linked record field. To do this you need an array of objects, as specified in the scripting documentation
I like to use an array map function for this.
Symptom: id_array.map(recordID => ({id: recordID}))
There are a few other unexpected things about your code, but hopefully this will give you enough to work with.
May 03, 2022 03:07 AM
That gives the following error:
x: Can't create records: invalid cell value for field 'Symptom'.
Duplicate linked record objects
at main on line 154
May 03, 2022 04:00 AM
@kuovonne - If it helps, this hard code works:
id_array.push(recordID)
Symptom: [{id: id_array[0]},{id: id_array[1]}],
But I am trying to do this via a loop instead of hardcoding
May 03, 2022 04:11 AM
Sounds like you have a duplicate value in your array.
May 03, 2022 04:27 AM
Hi,
I would recommend to use some variable (for example, name it ‘toCreate’) containing array
[
{fields: {
Client: [{id: clientID}],
Symptom: [{id: {id_array}]}
}]
and then
await table_Output.createRecordsAsync(toCreate);
await is mandatory for async functions
in case or error you can temporary comment record creation and set output for debug reasons:
output.inspect(toCreate)
//await table_Output.createRecordsAsync(toCreate);
but first of all, what function do you need?
into a createrecordasync function? - create single record
or
table_Output.createRecordsAsync - create multiple records
May 03, 2022 04:47 AM
Hi - Perhaps the question wasn’t clear, @Alexey_Gusev
It isn’t a problem with adding one term to different fields, the problem is adding multiple fields where there are multiple linked records in that field.
This hardcode works:
Symptom: [{id: id_array[0]},{id: id_array[1]}]
But I am having trouble looping it or knowing how to map it and add to the rest of the array.
May 03, 2022 04:50 AM
Note, @Alexey_Gusev this code you put did not work:
Symptom: [{id: {id_array}]}
As there are multiple values in the array.
May 03, 2022 04:51 AM
@kuovonne - the scripting allows for multiple values, so the problem is a syntax problem.
May 04, 2022 06:26 AM
@Rose_Haft1 The initial reply from @kuovonne should work. Are you certain that id_array is a string array? I recommend logging it to the console to confirm.
If you confirm that it’s a string array, could you please copy and paste the exact code you’re testing after applying Kuovonne’s solution? I recommend using the preformatted text styling option—the button that looks like </>
in the comment editor—to style your code more cleanly.