Hello community! I would like support to create a custom script that finds a specific:
1. Table
2. Record within that table
3. Cell within that record
And it removes an email (inked record) from that cell.
The 'email to be removed 'and 'RecordId' are updated automatically using input variables in the scripting code using input.config().
I would really appreciate any help with this. Thanks in advance!
I tried using ChatGPT to create the script but I kept getting errors. Here's an example of the script it provided:
// Airtable script to remove an email from a list of emails within a specific cell
// Replace 'YourTable' with your actual table name
const table = base.getTable('YourTable');
// Replace 'EmailsField' with your actual field name containing the list of emails
const emailsField = table.getField('EmailsField');
// Replace 'LinkedEmailsField' with your actual linked records field name for email addresses
const linkedEmailsField = table.getField('LinkedEmailsField');
// Extract the input variables from Automations
const { emailToRemove, recordId } = input.config();
try {
// Fetch the specific record
const records = await table.selectRecordsAsync({
filterByFormula: `RECORD_ID() = '${recordId}'`,
});
// Ensure a record is found
if (records.length === 0) {
throw new Error(`Record with ID ${recordId} not found.`);
}
// Get the first record from the fetched records
const specificRecord = records[0];
// Get the current list of linked emails in the specified cell
const currentLinkedEmails = specificRecord.getCellValue(linkedEmailsField);
// Ensure the field has a valid value and is an array
if (currentLinkedEmails && Array.isArray(currentLinkedEmails)) {
// Remove the specified email from the list
const updatedLinkedEmails = currentLinkedEmails.filter(email => email !== emailToRemove);
// Update the record with the modified list of linked emails
await table.updateRecordAsync(specificRecord, {
[linkedEmailsField.name]: updatedLinkedEmails,
});
console.log(`Email ${emailToRemove} removed successfully from the specified cell in record ${recordId}.`);
} else {
throw new Error(`Error accessing or updating the linked emails field for record ${recordId}.`);
}
} catch (error) {
console.error(error.message);
}