Establish Multiple Fields Within createRecordsAsync

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.
                },
            ]);

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.

2 Likes

It worked perfectly! Thank you!!

1 Like

Sam - Any idea how I would adjust this to add multiple records from the array?

Any help would be appreciated.

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!

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.

// 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++;
};
};