May 06, 2020 09:51 AM
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
May 06, 2020 10:30 AM
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
May 06, 2020 12:27 PM
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:
May 06, 2020 11:44 PM
@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é
May 07, 2020 12:07 AM
Jonathan, perfect, thanks for that, just what I needed to know.
K
May 07, 2020 01:06 AM
that is so useful, thanks - K
May 07, 2020 01:13 AM
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:
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:
Hope this helps!
JB
May 07, 2020 10:16 PM
That is a great reply, Jonathan. Thank you for taking the time to put this together. I like the approach.
Again, thank you!
André