Rename attachments using values from other field

Hi everyone, I have a bunch of records and have used the QR Code Generator extension to generate a QR Code and save it to an attachment field. The problem with this is that each QR code has the same filename (as there’s no option to rename the file), this makes it very confusing when downloading them all.

I have come across a script here : Automate changing the file name of an attachment

// Setup
let config = input.config()
let table = base.getTable(“:amphora: CONSIGNED ITEMS”)
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)

// Collect record data
let newPrefix = record.getCellValue(“AuctionLotNumber”)
let files = record.getCellValue(“Bidsquare Cloud QR”)

// Get the extension and add it to the new name
let newFiles =
for (let file of files) {
let parts = file.filename.split(“.”)
let ext = parts[parts.length - 1]
let fileNumber = “0” + (files.indexOf(file) + 1)
let newName = ${newPrefix}-${fileNumber}.${ext}
newFiles.push({url: file.url, filename: newName})
}

// Reattach the item with the new filename
await table.updateRecordAsync(record, {
“Renamed Bidsquare Cloud QR”: newFiles
})

However, I’m struggling to implement that solution (and couldn’t figure out how to restart or reopen that other topic). I am getting errors at:

let config = input.config()
let query = await table.selectRecordsAsync()

  • selectRecordsAsync is strikethrough

What I am trying to do is rename each QR Code using information from another field (called ‘AuctionLotNumber’ in the same Table (:amphora: CONSIGNED ITEMS).

Thanks in advance for the help!

Welcome to the community, @Kevin_Parmar! :smiley:

The reason for the strikethrough can be seen by hovering over it. Long story short, the method of building a record query that the script you found (written by me) uses is the old way. While it’s “simpler” in a sense in that it just grabs all fields for all records, it’s not very efficient with large datasets, so the Airtable devs have marked it as being “deprecated,” which is another way of saying “it’ll work for now but it’s going to disappear in the future so please figure out the better way ASAP.” :slight_smile: (Unfortunately I can’t edit anything from that other thread either because it’s been closed. That’s just one of several examples of “legacy” code hanging around the forum that sadly can’t be updated.)

Anyway, the docs for the selectRecordsAsync method give more appropriate examples of how to correctly use it. You can find the docs in the “API” section at the bottom of the scripting interface. At a minimum you need to pass an object containing a one or more options which will help you get back a more focused dataset, preferably starting with a specific list of fields from which to retrieve data. Using the field names from the example that you provided, the modified query line would look like this:

...
let query = await table.selectRecordsAsync({
    fields: ["AuctionLotNumber", "Bidsquare Cloud QR", "Renamed Bidsquare Cloud QR"]
})
...
1 Like

Right, that makes a lot of sense! Thanks for the script, your reply - and the explanation. Appreciate it.

Unfortunately ‘let config = input.config()’ still returns an error - says that there is “Expected 1 arguments but got 0.”. Apologies if it’s an obvious question / explanation - not much coding experience.

Thanks again.

Sorry for taking your thread in a new direction, but I thought it might be helpful for me to share my experience with QR codes.

I don’t know JavaScript either, yet I’ve setup a few clients with dynamically-named QR codes — without using any code at all — by using Make.com to automatically generate these QR Codes for them.

Make lets you dynamically set the name of the QR code upon generation, and it’s 100% free to use — as long as you stay under 1,000 operations per month.

Hers a screenshot of one way to generate QR codes with Make, although there are many different ways that you can approach this task:

That line shouldn’t be displaying an error, as it’s correctly constructed. Sometimes the error-checking system in Airtable’s script editing environments can mark things incorrectly if you’ve had Airtable (and/or your browser) open for a while. Try restarting the browser and see if that line is still marked as an error.

Hi, I even tried it in a different browser, still get the same error. The Run button is greyed out and has a message: “Your script has invalid settings defined. Edit the code to fix any errors.”

Thanks for that, I have tried it before and I need to create about 400 QR codes a month, so I burn through that quota pretty quickly =-/

Here is their pricing page. Free will get you 1,000 operations, and $9 will get you 10,000 operations.

This script looks like it was designed to run as an automation action. This error implies that you are trying to run the script from Scripting Extension. Can you verify how you are trying to run the script?

If you want to run the script from an automation, you will need to also setup the input variable.

If you want to run the script on demand from Scripting Extension, the script will need several lines reworked for the other environment.

1 Like

Hi, yes I am trying to use it in the scripting extension.

Thanks for the clarification (and sorry for not checking on that earlier). For future reference, that detail would have been helpful to know up front. Airtable’s two scripting environments—the Scripting extension and the “Run a script” automation action—are very similar, but there are important differences that can make a script written for one not work in the other, especially when it comes to the input mechanism.

I’m afraid that I’m short on time right now or else I’d go into the changes that @kuovonne mentioned. If anyone else is able to cover them, please do.

Oh right, sorry I didn’t even know that there were 2 different environments. I will give it a try in the Run a Script automation action. Thank you.

I have tried to use an automation to run a script, but I am now getting an error on : let record = query.getRecord(config.recordID)

Thanks.

Did you create the input variable named recordID in the left side of the editor and set the value to the record id of the triggering record?

No I did not :grimacing:

Thanks a lot! It works =-)

2 Likes