const targetAudienceTableName = "Target Audience SS";
// Access the record ID from the triggering record
const triggeringRecord = input.config();
const recordId = triggeringRecord.RecordModified;
console.log("Triggering Record ID:", recordId);
const targetAudienceTable = base.getTable(targetAudienceTableName);
// Use the selectRecordsAsync method to retrieve the specific record
const recordDetails = await targetAudienceTable.selectRecordsAsync();
// Assume recordDetails is an array of records obtained from Airtable
const filteredRecords = recordDetails.records.filter(record => (
record.getCellValue("Record ID") === recordId
));
// Check if the filteredRecords array is not empty
if (filteredRecords.length > 0) {
const targetAudienceRecord = filteredRecords[0];
// Get the values from the Lookup fields
const newslettersMediaCompanyValues = targetAudienceRecord.getCellValue("Newsletters Media Company");
const podcastMediaCompanyValues = targetAudienceRecord.getCellValue("Podcast Media Company");
const youtubeMediaCompanyValues = targetAudienceRecord.getCellValue("YouTube Media Company");
console.log("Newsletter", newslettersMediaCompanyValues);
console.log("Podcaast", podcastMediaCompanyValues);
console.log("YouTube", youtubeMediaCompanyValues);
// Combine values
const allMediaCompanyValues = [
...(newslettersMediaCompanyValues || []),
...(podcastMediaCompanyValues || []),
...(youtubeMediaCompanyValues || []),
];
// Unique linked record IDs
const uniqueLinkedRecordIds = new Set();
for (const value of allMediaCompanyValues) {
// Add the 'id' property to the Set to ensure uniqueness
uniqueLinkedRecordIds.add(value.id);
}
// Convert the Set to an array with objects having 'id' property
const linkedRecords = Array.from(uniqueLinkedRecordIds).map(id => ({ id }));
console.log("linkedrecord", linkedRecords);
// Log the unique values to the console for debugging
console.log(`Record ID: ${targetAudienceRecord.id}`);
console.log(`Unique Values: ${linkedRecords}`);
// Update the "Media Company" linked field
await targetAudienceTable.updateRecordAsync(targetAudienceRecord, {
"Media Company": linkedRecords,
});
console.log("Update completed.");
} else {
console.log("Record not found.");
}
Output:
CONSOLE.LOG
▶(10) n"rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2", "rechzDyJBzM03Zix2"]
CONSOLE.LOG
"Record ID: recFR6bmsXDr0MlxQ"
CONSOLE.LOG
"Unique Values: eobject Object]"
ERROR
Error: Field "fldFcoeRwS2SiTbLN" cannot accept the provided value.
at main on line 58