Help

Automate updating attachment names (but keeping the same image) with text from other fields in the record

Topic Labels: Scripting extentions
2391 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Jonathan_Cottre
4 - Data Explorer
4 - Data Explorer

I am trying to automatically update over 1,500 attachment names in fields within an attachment field where I only have a single image. Relevant to this post:

My table name is: “Books”
The attachment field (that I want to update the file names in) is: {Cover}
Single line text field: {Title}
Link to another record field, with multiple select options: {Author(s)}

Currently, each of the {Cover} file names are completely random and inconsistent. I want to incorporate the {Title} and {Author(s)} text into the new name of the {Cover} attachments, but keep the existing image from the {Cover} and not simply replace it.

The NEW name for the {Cover} attachments would be: “{Title} by {Author(s)} - MySiteName”

If there are multiple authors, ideally, it would have an “and” between their names, but if this is too difficult, the {Title} alone is more than enough to make me satisfied. I have my automation set to “Update record when {Cover} is not empty”, but I can’t figure out the script for the life of me.

I’ve read a couple other community posts about this, but am new to scripting and can’t figure it out based on the other examples provided. Can anybody help me create this script?

Thank you in advance!

5 Replies 5
Jono_Prest
6 - Interface Innovator
6 - Interface Innovator

Hi @Jonathan_Cottrell, give this a try:

let nameOfTable = "Books"
let nameOfAttachmentField = "Cover"
let nameOfAuthorsField = "Author(s)"
let nameOfTitleField = "Title"

let table = base.getTable(nameOfTable)
let recordQuery = await table.selectRecordsAsync()

for (let record of recordQuery.records) {
    let attachmentField = await record.getCellValue(nameOfAttachmentField)

    if (attachmentField) {
        let title = await record.getCellValueAsString(nameOfTitleField)
        let authors = await record.getCellValue(nameOfAuthorsField)
        if (authors) {
            authors = authors.map(author => author.name)
            authors = authors.join(" and ")
        } else {
            authors = ""
        }

        let newTitle = `${title} by ${authors} - MySiteName`

        await table.updateRecordAsync(record, {
            [nameOfAttachmentField]: [
                { url: attachmentField[0].url, filename: newTitle }
            ]
        })

    }
}
Andrea_Seisler
5 - Automation Enthusiast
5 - Automation Enthusiast

I have a similar issue, but instead of the new title completely replacing the old title, I need to prepend it to the name of the old title.

So I changed:

let newTitle = `${title} by ${authors} - MySiteName`


to

let newTitle = `${title}-${authors}-${attachmentField}`

However the current name of the attachment (e.g. current_name.pdf) is not part of the new file name.

I ended up with

TitleofBook-AuthorOfBook-[object Object],[object Object]

instead of

TitleOfBook-AuthorOfBook-current_name.pdf

I have just started scripting in Airtable. Any suggestions would be greatly appreciated.

Hi,

attachment cell value is array of objects.
while you need a string.
in you case, if your issue is similar and there is only one file per cell, it’s still object looks like this:

image

So, you should at first choose element of array:
${attachmentField[0]} , and then choose
property. it seems like you need filename. So, correct call of this variable will be:
${attachmentField[0].filename}

Andrea_Seisler
5 - Automation Enthusiast
5 - Automation Enthusiast

Thank you. I am beginning to understand the syntax.

So if I have varying number of attachments, usually between 1 and 4 how would I do this? I am not sure where a loop would be required. This code will delete any attachments beyond 1.

Kind regards,
Andrea

I got it. I needed a for loop to go through the attachment names:

let newAttachments =
for (let attachment of attachmentField) {

        let newTitle = `${title}-${authors}-${attachment.filename}`
       
      
        //push each new attachment to the new array created
        newAttachments.push({url: attachment.url,filename: newTitle})
            }


    await table.updateRecordAsync(record, {
        //update the record with the array that contains all of the renamed attachments in the cell
        [nameOfAttachmentField]: newAttachments
    })