I am working to automate a process that feels like I should be able to script but have been struggling. Admittedly I am new to scripting so I may not be understanding how to structure this, I have asked around, tried to get an assist from Chat GPT, and poked and prodded this along but the final outcome has eluded me. Let me break down the intended function and drop the code in. Any help is greatly appreciated.
Intended function:
The automation is initiated when a record enters the "Meetings" view of the "Connections" table, there is a text field called "Attendee Emails" where a formula has collected and concatenated emails using ", " as a delimiter. We are passing that through to the script as an input variable called "attendeeemails". What we need is for the automation to go through the string stored in "attendeeemails" and pull each email out one by one, and check each individual email against the "Email" field in the "Full view" in the "People" table. When any individual email item from "attendeeemails" matches a value in the "Email" field in the "Full view" in the "People" table I would like to retrieve the corresponding record ID, I want the matching record IDs to be used to link the corresponding records in the "Attend/Invite/Include" Linked record field in the Connections table.
Script:
// Import the Airtable API
let connectionsTable = base.getTable("Connections");
let peopleTable = base.getTable("People");
// Get the script input for attendeeemails
let scriptInput = input.config();
let attendeeEmails = scriptInput ? scriptInput.attendeeemails : null;
// Check if the "attendeeemails" input is provided
if (!attendeeEmails) {
console.error("Script input 'attendeeemails' is missing.");
return;
}
// Split the attendeeEmails string into an array of individual emails
let emailsArray = attendeeEmails.split(", ");
console.log("EmailsArray output:", emailsArray)
// Initialize an array to store the matching record IDs
let matchingRecordIDs = [];
// Iterate through each email in the array
for (let email of emailsArray) {
// Query the People table for records with a matching email
let queryResult = await peopleTable.selectRecordsAsync({
filterByFormula: `{Email} = "${email}"`
});
// If a matching record is found, retrieve and store its ID
if (queryResult.records.length > 0) {
matchingRecordIDs.push(queryResult.records[0].id);
}
}
console.log("Matching Record IDs:", matchingRecordIDs);
// Get records from the "Meetings" view of the "Connections" table
let view = connectionsTable.getView("Meetings");
let records = await view.selectRecordsAsync();
// Iterate through each record in the view
for (let record of records.records) {
// Update the "Attend/Invite/Include" Linked record field in the "Connections" table
await connectionsTable.updateRecordAsync(record, {
"Attend/Invite/Include": matchingRecordIDs.map(id => ({ id }))
});
}
// Log the result
console.log("Linked records updated successfully!");