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.
May 05, 2022 03:04 AM
I would use mentioned function ‘map’, which loops through array of IDs and returns array of objects like {id: recordID}
const symptom=id_array.map(recordID => ({id: recordID}))
const toCreate={fields: {Client: [{id: clientID}], Symptom: symptom} }
May 16, 2022 04:29 AM
Thanks @kuovonne and everyone. It is working:
May 16, 2022 06:11 AM
Glad you got it working. Do you mind sharing what specifically solved your issue? (It can be hard to identify the key concept in an entire base share.) This could help anyone else with a similar issue who later reads this thread.
May 16, 2022 10:47 AM
Here is the code I used. It worked when I did what you asked. I originally thought you meant to do the mapping in the loop, so it wasn’t working but it worked when I put it in the createrecords command:
var t_links = base.getTable("Table 1")
var t_links_aw = await t_links.selectRecordsAsync
var t_table = base.getTable("Test linking")
var t_table_aw = await t_table.selectRecordsAsync
var arr_id = []
arr_id.push("recmi56Y9WQiRd3B5")
arr_id.push("recOQdZvyp7AKDOFm")
arr_id.push("rec4cHGYeA1S6p9y1")
t_table.createRecordAsync({
Name: "This is a name",
linked: arr_id.map(recordID => ({id: recordID}))
})