Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Establish Multiple Fields Within createRecordsAsync

Topic Labels: Scripting extentions
5070 9
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Alfieri
4 - Data Explorer
4 - Data Explorer

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.
                },
            ]);
9 Replies 9
Sam_Cederwall
7 - App Architect
7 - App Architect

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.

Michael_Alfieri
4 - Data Explorer
4 - Data Explorer

It worked perfectly! Thank you!!

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!

Mark_Wang
4 - Data Explorer
4 - Data Explorer

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

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

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.

Great! Now how can you do it with a loop so you don’t have to know all the linked fields in the code?