Help

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

Re: Help with an Airtable Automations Script

880 0
cancel
Showing results for 
Search instead for 
Did you mean: 
matt66hew12
4 - Data Explorer
4 - Data Explorer

i am attempting to use the attached code within an airtable automations run script tied to a "When Record Enters View" trigger. I have almost zero background in JS, and I just can't get this thing to work. Do any of you have any suggestions as to how I can fix these issues?

6 Replies 6

Hi,
please describe what do you want to do with the record.

matt66hew12
4 - Data Explorer
4 - Data Explorer

In short I have two tables, and what I essentially need this script to do is to look into a multi-select field named "Client" and pull all the values, and then I need it to look into a single select field named "Group Name" and pull its value. Then I need the script to look into the other table and identify the record that has a match with the client name in the field "Name". If it finds a record with a matching name, then I need the script to find the checkbox field where the name of the field matches the string in the first table's "Group Name" field, and then set that field's value within that specific record to True. I need this to do this for however many client names there may be in the multi select field "Client".

matt66hew12
4 - Data Explorer
4 - Data Explorer

If anyone is interested, I was able to solve this particular problem using the following formula:

// Fetch the input configuration from the automation
let inputConfig = input.config();

// Extract the triggering record details from the input configuration
let recordId = inputConfig.recordId;
let groupName = inputConfig.groupName;
let clientNames = inputConfig.clientNames;

console.log("Record ID:", recordId);
console.log("Group Name:", groupName);
console.log("Client Names:", clientNames);

// Define the table references
let clientsTable = base.getTable("Clients");

if (!recordId || !groupName || !clientNames || clientNames.length === 0) {
console.error("Missing necessary inputs from the automation trigger.");
return;
}

// Fetch all client records
let allClients = await clientsTable.selectRecordsAsync();

// Iterate over each selected client name
for (let clientName of clientNames) {
// Search for the client record in the Clients table
let clientRecord = allClients.records.find(record => record.getCellValue("Name") === clientName);

// If a matching client record is found
if (clientRecord) {
// Determine which Parenting field to update based on the group name
let parentingField = `Parenting ${groupName.replace("Parenting ", "")}`;

// Update the Parenting field in the client record
let updateData = {};
updateData[parentingField] = true;

await clientsTable.updateRecordAsync(clientRecord.id, updateData);
console.log(`Updated ${clientName} with ${parentingField}`);
} else {
console.error(`Client '${clientName}' not found.`);
}
}

@matt66hew12 wrote:

If anyone is interested, I was able to solve this particular problem using the following formula:


Thank you for sharing your solution. You state that you have almost zero background in JS, so it might be helpful to other people who are also new to JS to see an example of what it takes to get a working script. For example, how was your original script created? (It looks like AI might have been involved in its creation.) Your revised script (that you call a formula) looks very different from the script in your screen shot. Did you adjust the original script until it worked, or did you start from scratch? Did you get help from AI at any point in the process? If so, what types of prompts and which AI were the most useful? Did you get help from a human in the process?

Also note that if your script finds many matching records to update, you might run into automation runtime limits because you are updating records one at a time instead of in batches.

matt66hew12
4 - Data Explorer
4 - Data Explorer

@kuovonne I just had to walk AI out of the logic loop it was in by going line by line on the errors. How would I adjust the code to update in batches rather than individually?

Thank you for explaining how you used AI to revise the script.
This example script in the documentation shows one way of updating records in batches. Instead of using updateRecordAsync() inside the loop, you create an array of updates inside the loop, and use updateRecordsAsync() after the loop.