Help

Re: Automate changing the file name of an attachment

905 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Cameron_Goldber
6 - Interface Innovator
6 - Interface Innovator

Hey Airtable Community.

I need help with automating the repetitive task of manually changing the name of a file after it’s been uploaded into my base. As of right now, we have a user filling out a form, and after it’s submitted, I want that to trigger the name changing. I have a field that is automatically created with a formula based on information captured in the form that I want the name changed too.

I’ve been looking at using the Airtable automation feature, but after trying a bunch of things, I can’t get it to work (perhaps I’m just too new at this) I also feel like maybe using a script might be the answer, but I don’t know how to do that, and if that’s the path to get this to work, I’ll need your help too :slightly_smiling_face: . Ultimately, I’m not sure and was hoping you (the community) might know.

Thanks in advance everyone!

26 Replies 26

Found the solution... For those who are interested, check my video above to see the fields and the base structure to understand...


The inputs used from input.config() were:
recordID
newName

 

The rest is not necessary. There are some comments that explain the code, use google translator from Portuguese PT-BR to your language.

 

// Setup
let config = input.config()
let table = base.getTable("Lista mestra")

// Define a variável newName e atribui a ela o valor Nome ajustado
let newName = config.newName
console.log(config.newName)

// Cria o vetor para armazenar as infos do registro submetido
const record = await table.selectRecordAsync(config.recordID)
console.log(record)

// @ts-ignore > Ignora o erro devido a possibilidade do registro ser nulo, pois as condições gatilho da automação garantem que não será.
// Armazena as informações do(s) anexos no vetor attachmentField
let attachmentField = record.getCellValue("Documento")

// Cria um novo vetor vazio que receberá o(s) documento(s) renomeados para sobrescrever os existentes com nome original
let newAttachment =[]

// Vai executar o for para a quantidade de anexos que houver dentro do vetor attachmentField
for (let [attachmentNumber, attachment] of attachmentField.entries()){
    console.log(attachmentNumber,attachment,)

    // Quebra a string do nome original do anexo em um vetor onde a extensão está separada em um endereço desse vetor
    let findExtension = attachment.filename.match(/\.\w+$/) || [""]
    let extension = findExtension[0]
    console.log(extension)

    // Se o número de anexos é maior que 1, então ele renomeará os anexos e acrescentará um incrementador sequencial ao final
    let fileSpecifcName = attachmentNumber>0 ? "_" + (attachmentNumber+1) : ""

    // Alimenta o vetor newAttachment com a url e o nome do anexo com os ajustes feitos
    newAttachment.push({
        url:attachment.url,
        filename: newName + fileSpecifcName + extension
        })
}
console.log("New attachment field array", newAttachment)

// @ts-ignore > Ignora o erro devido a possibilidade do registro ser nulo, pois as condições gatilho da automação garantem que não será
// Atualiza o campo Documento com o(s) anexo(s) modificado(s)
await table.updateRecordAsync(record.id,
{
    Documento: newAttachment
})

 

Hi @Justin_Barrett,

The code you've provided works extremely well in changing the filename. 👍 The only issue is a change in the image somehow. The image no longer shows in the attachment field and when opened within Airtable, this is what I see:

Image  Name Update.jpg

 

I am using Firefox and have had no issues with viewing any attachment type within Airtable.

I'm sorry that you're running into issues, but I'm swamped at the moment and don't know when I'll have time to get around to looking into this. Not sure if the post above yours would help, but @greenant said that the problem was solved with the code that they shared.

I don't recall if I mentioned this before, but I'm wondering if this is due in part to last year's update that changed how URLs for files in attachment fields are handled by Airtable.

Again, I'm sorry that I can't help more on this, but life is drastically different for me from what it was a couple of years ago when this first came up, and I'm afraid that I no longer have the time to do deep dives into issues like this.

siliceous
6 - Interface Innovator
6 - Interface Innovator

No worries @Justin_Barrett 

Thanks for replying. I couldn't get @greenant's code to work, unfortunately.

Alvaro_Hernande
6 - Interface Innovator
6 - Interface Innovator

Muito obrigado @greenant !

The code is working perfectly!

And also thanks to @Justin_Barrett , always helping us!!

😀

Katieglamer
5 - Automation Enthusiast
5 - Automation Enthusiast

I found that using the expiring download URL instead of the URL stopped the file from breaking, I'm really not sure why though

Richard_Wright
4 - Data Explorer
4 - Data Explorer

@greenant Thanks a Million

And thanks Heavens for rollback 😎