Help

Re: Automated file uploads upload with wrong filename

168 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Jayjayhui
4 - Data Explorer
4 - Data Explorer

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}`);
}

 

 

 

2 Replies 2
LastSamuraj
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi there,

I am currently having almost the same problem as you. My script sendig uploaded pictures to tinyjpg and after they came back the files get new naming. For a few days now the naming process has stopped working. I don't understand why because I haven't changed anything.

This is my code to rename the files:

if (attachments && attachments.length > 0) {
    // Iterate through each attachment in the field.
    for (let [i, attachment] of attachments.entries()) {
      let newFileName = `${baseFileName}_${i + 1}`; // New file name based on "Name" and the attachment number
      let recordAttachmentUrl = attachment['url'];

      console.log(`Compressing ${baseFileName} (Image ${i + 1})`);

I suspect at this point that Airtable has changed something and our code is no longer valid.

Hmm interesting, I just tried the following and it seemed to work fine:
Screenshot 2024-10-26 at 2.33.58 PM.png

Screenshot 2024-10-26 at 2.34.16 PM.png

await table.updateRecordAsync(recordId, {
    "Attachments 2": [{url: attachment.url, filename:"test"}]
})

Tried it as both a script extension and as an automation script