Help

Discover what data silos are costing your org in our commissioned Forrester study. Learn more

Changing the file names of multiple attachments per record

Topic Labels: Automations
Solved
Jump to Solution
228 2
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi all,

I’d appreciate some guidance on how can I change the filenames of my attachments. I have a table with 500 records. Every record has a name and multiple images (attachments).

image

The attachments have identical file names in multiple records, e.g I might have a file called 1.jpg in one record, then a different attachment in a different record with the same name of1.jpg.

I managed to find a script that does that, but it changes the name only for one attachment. Ideally, would be to create a rule that adds to the current filename, the name from another field.

e.g: Gorge 1 + current filename.jpg

// Setup
const mainTable = base.getTable("Test")
const mainQuery = await mainTable.selectRecordsAsync()

// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
    let attachments = record.getCellValue("images")
    if (attachments.length) {
        updates.push({
            id: record.id,
            fields: {   
                "images": [{
                    url: attachments[0].url,
                    filename: record.getCellValue("Name")
                }]
            }
        })
    }
}

// Update records
while (updates.length) {
    await mainTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}


Thank you in advance,
Victor

1 Solution

Accepted Solutions

Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for

And here’s the code:

// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
    fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})

// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
    let name = record.getCellValue(mainTable_nameFieldName)
    let attachments = record.getCellValue(mainTable_imageFieldName)
    if (attachments.length) {
        for (let attachment of attachments){
            let attachmentName = name + " - " + attachment.filename

            updates.push({
                id: record.id,
                fields: {   
                    [mainTable_imageFieldName]: [{
                        url: attachment.url,
                        filename: attachmentName
                    }]
                }
            })
        }
    }
}

// Update records
while (updates.length) {
    await mainTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

See Solution in Thread

2 Replies 2

Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for

And here’s the code:

// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
    fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})

// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
    let name = record.getCellValue(mainTable_nameFieldName)
    let attachments = record.getCellValue(mainTable_imageFieldName)
    if (attachments.length) {
        for (let attachment of attachments){
            let attachmentName = name + " - " + attachment.filename

            updates.push({
                id: record.id,
                fields: {   
                    [mainTable_imageFieldName]: [{
                        url: attachment.url,
                        filename: attachmentName
                    }]
                }
            })
        }
    }
}

// Update records
while (updates.length) {
    await mainTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

Wow, thanks a lot, Adam! Didn’t know this community is so helpful! Have a great day!