Help

Setting Attachment URL dynamically

5316 7
cancel
Showing results for 
Search instead for 
Did you mean: 
Kevin_Carter
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello there, newish to AT, i wonder is there a way to set the URL of the attachment field dynamically using code ? i have used the formula field to build a URL to an image, and i know wish to set the URL of the attachment field to that URL ? doable ? thanks in advance for any help you can offer.

Kevin

7 Replies 7

Hi @Kevin_Carter - no you can’t do this directly, but there are at least 2 ways (probably more) to get the result you want.

First step is to get a formula field with the attachment URL as the value (sounds like you already have this).

Then, option 1 is to use a service like Zapier to automate the updating of an attachment field using the URL as the source for the attachment. Not sure if you have used Zapier before, but this flow is a bit more complicated than many of the “regular” Zaps. Essentially you need to trigger the Zap with “New record” or “New record in a view” (I prefer the latter for this sort of thing because it is easier to control). Then you need to “find” the record and, as a 3rd step, “update” the record (update the attachment field with the source URL).

The end result is the image is attached, as you are trying to get to.

The 2nd option is to have a script - either using the new scripting block or another external script - which does the same sort of process in code (using the API or the built-in methods for the scripting block).

JB

Here’s simple script you can use in the scripting block that does what you need:

let table = base.getTable('Table 1');

let query = await table.selectRecordsAsync();

for (let record of query.records) {
    // if the attachment field is empty
    if(record.getCellValue('Attachment') == null) {
        let recordId = await table.updateRecordAsync(record, {
            'Attachment': [
                { url: record.getCellValue('URL')}
            ]
        })
    }
}

Just change the names of Table 1, Attachment and URL to be the names of your table, attachment field and URL formula field.

Try it out on a copy of your base first to make sure it does what you want :slightly_smiling_face:

@JonathanBowen Hi Jonathan, I would love to dive into scripting, but I don’t know where to start. Is there a simple course I can follow?

Regards,
André

Jonathan, perfect, thanks for that, just what I needed to know.

K

that is so useful, thanks - K

Hi @Andre_Zijlstra - great question! I know from past experience that it can be hard to get going on these things. The Airtable scripting language, as you might know, is JavaScript, so any resources on this will be useful. I would particularly recommend this free Codecademy course - Introduction to JavaScript

Freecodecamp is also a great resource - scroll down to the JS section:

Screenshot 2020-05-07 at 08.47.46

Within these two resources, there’s tons of guides for basic and more complex JS examples and “how-tos”.

However, the scripting block utilises a combination of pure JS, e.g.:

for (let record of query.records) {
  // do something
}

and some methods that are provided by and are specific to Airtable, e.g.:

let table = base.getTable('Table 1');

In the early days, at least in my experience, when looking at example scripts it takes a little time to get your head around what is an AT method, what is JS and how the two meet.

A few recommendations:

  • Just keep writing scripts. Try writing scripts for things that you know you could do with a formula or do manually. For example, set up a base with two number fields and write a script that adds these together and outputs the result (console.log is your best friend). Then enhance the script so that it add the numbers and writes the result to a 3rd number field. These simple scripts will provide a great foundation for moving onto more complex scripts and will help solidify the AT methods - defining a table, getting results, writing to a field etc.
  • When you are learning JS on its own, use the Airtable scripting block as your code environment (this is easier than running in a browser console and simpler to set up than an HTML file with JS in it). You can use the block to run JS on its own and it doesn’t need to interact with AT at all.
  • Scripts are often posted on the forum - copy them into your own base and run them step by step to see what they are doing. I came across a script the other day that I couldn’t really understand just by looking at it. There was a section in the middle of the script that didn’t make sense, so I copied the whole script to my own base, commented out everything below the confusing block, then logged the output of this block to the output panel (using console.log). The result of the block was hidden when the script was run in its entirety, but by breaking it out, I was able to understand what this section was doing and I learnt something in the process.

Hope this helps!

JB

That is a great reply, Jonathan. Thank you for taking the time to put this together. I like the approach.
Again, thank you!

André