Save the date! Join us on October 16 for our Product Ops launch event. Register here.
May 12, 2020 02:12 PM
Trying to add more than one field under the createRecordsAsync action. This is my single field being created right now, which is pulling (successfully) the name from an array. What I’m trying to do is perhaps a tad loftier than I’m indicating, but I’d like to populate the linked record field of the created record with the record ID of another record. Can I do that in this step here?
await table.createRecordsAsync([
{
fields:
{"Name": tempArray[a]},
//hoping to insert the link via record ID at this stage here.
},
]);
May 12, 2020 03:04 PM
Hello,
I recently accomplished something similar to what I think you’re asking for. If I understand correctly, when creating a record, you would like record to already have a linked field populated. Is this correct?
In that case, you can access the id of a record by doing something like this (assuming you have queried your records to a variable named query):
for(let record of query.records){
let recID = record.id
}
Then, when creating records, I think you might want it to look like this:
await table.createRecordsAsync([
{
fields: {
"Name" : tempArray[a],
"Linked Field" : [{id:recID}]
}
}
]);
As a side note, I only started with JavaScript when the block came out so my expertise is limited but seeing as I just did something similar I thought I might be able to help. Hopefully I was able to capture what you needed. Best of luck.
May 12, 2020 03:19 PM
It worked perfectly! Thank you!!
Oct 12, 2020 07:25 PM
Sam - Any idea how I would adjust this to add multiple records from the array?
Any help would be appreciated.
Oct 14, 2020 09:56 AM
Hi, welcome to the community! If you’re talking about just doing a few different records you could do something like this:
await table.createRecordsAsync([
{
fields: {
"Name" : tempArray[a],
"Linked Field" : [{id:recID}]
}
},
{
fields: {
"Name" : tempArray[b],
"Linked Field": [{id:recID}]
}
}
]);
The only requirement here to create multiple records at once is to separate the object containing the field values by a comma.
Alternatively, you could push your desired values into an array and create records from that like the example below, which doesn’t use the same values as the example above but hopefully gives an idea of how it works.
//Tables
var table = base.getTable("Table");
var table2 = base.getTable("Table 2");
//Queries
var query = await table.selectRecordsAsync();
//Empty array
var recArray = [];
//Loop
for(let record of query.records){
//Variables
let field1 = record.getCellValue("field 1");
//Push fields to array
recArray.push({
fields: {
"Name": field1,
"Linked Record": {id: record.id}
}
});
}
//Create records
await table.createRecordsAsync(recArray);
The only thing to note here is if you begin creating more than 50 records at once. In this case, you will need to batch the process like so:
while (recArray.length > 0){
await table.createRecordsAsync(recArray.slice(0,50));
recArray = recArray.slice(50);
}
Essentially, what this does is takes the array that you want to create records from, creates the first 50 in the array and then removes the first 50 in the array and restarts the loop until there are no more records left in the array.
Hope that this helps! Good luck!
Oct 14, 2020 01:34 PM
Sam - Thanks for the help. I ended up finding the solution (after 10 or so hours of just trying different things), and it was essentially your second suggestion.
Oct 14, 2020 01:37 PM
// Pulling the record IDs of all TRACKED recipients
for (i = 0; i <= aryRecipients.length; i++) {
// Calling a function to check the email address, if there’s a match, the record ID is returned
let RecipientID = await LookupContact(aryRecipients[i]);
// If a record ID is returned
if (RecipientID != null) {
// Push the value into the object array
objTrackedRecipients.push({id: RecipientID});
// Next record
intAryTRCount++;
};
};
May 03, 2022 03:50 AM
Hi - How do you do this if you have multiple linked objects in the same record via a loop for instance:
await table.createRecordsAsync([
{
fields: {
“Name” : tempArray[a],
“Linked Field” : [{id:recID}],
“Linked Field” : [{id:recID2}],
“Linked Field” : [{id:recID3}],
}
}
]);
May 03, 2022 08:38 AM
Hi Rose,
To update a field with multiple linked objects you would do the following:
await table.createRecordsAsync([
{
"fields": {
"Name": tempArray[a],
"Linked Field": [{id: recID},{id:recID2},{id:recID3}]
}
}
]);
In this example, we are pushing an array of objects to the linked field, so instead of trying to write to “Linked Field” multiple times (which will throw an error), we write to the field once, attaching a list of linked records.
May 03, 2022 09:25 AM
Great! Now how can you do it with a loop so you don’t have to know all the linked fields in the code?