I have a script running that has been working well, but 2 days ago it stopped working.
Where it used to take a file from 1 column, rename it, and reupload it to a next column, it now:
1) Takes the file
2) Renames it
3) But when it uploads on airtable, the filename becomes a string of gibberish like "70d5b867" and doesn't use the filename I had specificed. The puzzling thing is, in the record edit history I see that the file with the right filename has been uploaded. But when I actually find the file, it's not named right.
Here's the script:
// Dynamically retrieve the record ID from input
let recordId = input.config().recordId;
let table = base.getTable("Agenda");
let record = await table.selectRecordAsync(recordId);
// Define an array of sets with their respective WIP tray field, output tray field, and naming format
let sets = [
{
wipField: 'WIP IF Session Banner (Automated)',
outputField: 'IF Session Banner (Automated)',
namingFormat: 'if24_session_banner_${sessionSlug}.png'
}
];
// Check if the record exists
if (record) {
// Loop through each set
for (let set of sets) {
let files = record.getCellValue(set.wipField);
// Ensure the files array is valid before proceeding
if (files && files.length > 0) {
let newAttachments = [];
for (let file of files) {
let url = file.url;
console.log(url);
// Get 'Banner file name slug' and construct the new filename for each set
let sessionSlug = record.getCellValue('Banner file name slug') || "default-slug"; // Handle empty slug
let newFileName = set.namingFormat.replace('${sessionSlug}', sessionSlug); // Use dynamic slug and naming format
console.log(newFileName);
newAttachments.push({ url: url, filename: newFileName });
}
// Update the record if there are new attachments
if (newAttachments.length > 0) {
let update = {};
update[set.outputField] = newAttachments;
await table.updateRecordAsync(record.id, update);
}
} else {
console.log(`No files found for record: ${record.id} in ${set.wipField}`);
}
}
} else {
console.log(`Record not found: ${recordId}`);
}