Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Formula or automation to add one image to an attachment field

Solved
Jump to Solution
2643 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Andrew_Davies
7 - App Architect
7 - App Architect

Hello

Wondering if someone can help me with something I am struggling with - but thought would be easy!

I have an attachment field in one of my tables, and most records have upwards of 10 images.  I'd like to set up a sync'd base which only shows the first image in the array.

I created a second attachment field called - "Main image" - but for the life of me, I can't work out how to automatically populate that field, with just the first image from the attachment field.

Any ideas?

Many thanks,

Andrew

1 Solution

Accepted Solutions
Ben_Young1
11 - Venus
11 - Venus

I've never been the biggest fan of Airtable's handling of attachments, but here's a quick workaround that you can use to grab the first attachment.

Here's a quick example I threw together for the sake of this post.

Ben_Young1_0-1675705767239.png

I have created a new Main Image attachment field that will mirror my first attachment in the Attachments field.
Next, I created a formula field with the following formula:

IF(
    {Attachments},
    IF(
        REGEX_MATCH(
            {Attachments}, ","
        ),
        REGEX_EXTRACT(
            REGEX_REPLACE(
                REGEX_EXTRACT(
                    {Attachments},
                    "^.*,"
                ),
                ",",
                ""
            ),
            "https://dl.airtable.com/.+[^)]"
        ),
        IF(
            {Attachments},
            REGEX_EXTRACT(
                {Attachments},
                "https://dl.airtable.com.+[^)]"
            )
        )
    )
)

If you weren't already aware, when you point a formula to just return the value of an attachment field, it will return an array of attachments in the format of:

file_name.extension (https://dl.airtable.com/.attachments/.../.../file_name.extension),
file2_name.extension (https://dl.airtable.com/.attachments/.../.../file2_name.extension)

 When I plug in that formula into a formula field, it will return the location of the first attachment in the array.

Ben_Young1_1-1675706119644.png

From here, I'll create a new automation that is triggered whenever the formula field is updated.
I'll create two conditional action groups.
The first group will fire only when the formula field is not empty. If it is not empty, then it will update the Main Image attachment field with the string value found in the Primary Attachment Link formula field.
The second group will fire only when the formula field is empty. If it is empty, then it will clear the Main Image attachment field. Since the formula will only return empty if there are no attachments in the Attachments field, we can safely assume that there shouldn't be any images displayed at all, so no cause for concern.

Here's what all the automation steps look like:

Ben_Young1_2-1675706509251.png

Ben_Young1_3-1675706530169.png

Here's what the automation behavior looks like:

Ben_Young1_4-1675706587468.png

 

Ben_Young1_6-1675706716167.png

This is a fast and simple way of doing it.
If you're comfortable with writing some quick JavaScript, I would highly recommend that you script this whole process instead.
I highly discourage the creation and use of fields that do not serve a workflow purpose to users, as they contribute to a bloated schema and create dependencies that can otherwise be avoided.
Nonetheless, this can serve as a quick solution if you don't care about the field.

See Solution in Thread

2 Replies 2
Ben_Young1
11 - Venus
11 - Venus

I've never been the biggest fan of Airtable's handling of attachments, but here's a quick workaround that you can use to grab the first attachment.

Here's a quick example I threw together for the sake of this post.

Ben_Young1_0-1675705767239.png

I have created a new Main Image attachment field that will mirror my first attachment in the Attachments field.
Next, I created a formula field with the following formula:

IF(
    {Attachments},
    IF(
        REGEX_MATCH(
            {Attachments}, ","
        ),
        REGEX_EXTRACT(
            REGEX_REPLACE(
                REGEX_EXTRACT(
                    {Attachments},
                    "^.*,"
                ),
                ",",
                ""
            ),
            "https://dl.airtable.com/.+[^)]"
        ),
        IF(
            {Attachments},
            REGEX_EXTRACT(
                {Attachments},
                "https://dl.airtable.com.+[^)]"
            )
        )
    )
)

If you weren't already aware, when you point a formula to just return the value of an attachment field, it will return an array of attachments in the format of:

file_name.extension (https://dl.airtable.com/.attachments/.../.../file_name.extension),
file2_name.extension (https://dl.airtable.com/.attachments/.../.../file2_name.extension)

 When I plug in that formula into a formula field, it will return the location of the first attachment in the array.

Ben_Young1_1-1675706119644.png

From here, I'll create a new automation that is triggered whenever the formula field is updated.
I'll create two conditional action groups.
The first group will fire only when the formula field is not empty. If it is not empty, then it will update the Main Image attachment field with the string value found in the Primary Attachment Link formula field.
The second group will fire only when the formula field is empty. If it is empty, then it will clear the Main Image attachment field. Since the formula will only return empty if there are no attachments in the Attachments field, we can safely assume that there shouldn't be any images displayed at all, so no cause for concern.

Here's what all the automation steps look like:

Ben_Young1_2-1675706509251.png

Ben_Young1_3-1675706530169.png

Here's what the automation behavior looks like:

Ben_Young1_4-1675706587468.png

 

Ben_Young1_6-1675706716167.png

This is a fast and simple way of doing it.
If you're comfortable with writing some quick JavaScript, I would highly recommend that you script this whole process instead.
I highly discourage the creation and use of fields that do not serve a workflow purpose to users, as they contribute to a bloated schema and create dependencies that can otherwise be avoided.
Nonetheless, this can serve as a quick solution if you don't care about the field.

So sorry for not replying!  This is perfect. Thank you.