Help

How to extract all URLs in a single attachment

Topic Labels: Scripting extentions
Solved
Jump to Solution
5965 14
cancel
Showing results for 
Search instead for 
Did you mean: 
kennypurnomo
7 - App Architect
7 - App Architect

Hello,

Can someone help me to write a code to extract all the URLs associated with my attachment in a particular record?

As you can see here, i have 3 photos where i need to generate “https://dl.airtablexxx1,https://dl.airtablexxx2,https://dl.airtablexxx3” in a field named “joined”

image

Thank you for helping me

14 Replies 14

Sorry, didn’t check for that. Here you go:

const table = base.getTable("Table 5")
const recordsQuery = await table.selectRecordsAsync();

let updateArray = [];

// iterate over all records
recordsQuery.records.forEach(record => {
    const attField = record.getCellValue("Attachment 1");

    // iterate over object and save as url
    if(attField) {
        let urlArray = [];
        attField.forEach(item => urlArray.push(item.url))
        const urlString = urlArray.join(', ')

        updateArray.push({id: record.id, fields: {"Joined": urlString}})
    }
})

// update records
while (updateArray.length > 0) {
    await table.updateRecordsAsync(updateArray.slice(0, 50));
    updateArray = updateArray.slice(50);
}

Strikethrough syntax is fine

Wow. This is the solution. Thanks A lot

@Rupert_Hoffschmidt, unfortunately this script only works if the “Joined” field is initially empty.

When i update the images in “attachment” field, running this script doesn’t change what inside the “Joined” field.

Is there any script line should i put so that it always updates?

Hi Kenny, yes it only works for adding attachments, but not deleting attachments from the field. I’ve added some code (if attField == null...) for deleting whatever is in the field IF the attachment is empty. I’ve tested all possible combinations and it seems to work well:

const table = base.getTable("Table 5")
const recordsQuery = await table.selectRecordsAsync();

let updateArray = [];

// iterate over all records
recordsQuery.records.forEach(record => {
    const attField = record.getCellValue("Attachment 1");

    // iterate over object and save as url
    if(attField) {
        let urlArray = [];
        attField.forEach(item => urlArray.push(item.url))
        const urlString = urlArray.join(', ')

        updateArray.push({id: record.id, fields: {"Joined": urlString}})
    }
    if(attField == null) {
        updateArray.push({id: record.id, fields: {"Joined": ""}})
    }
})

// update records
while (updateArray.length > 0) {
    await table.updateRecordsAsync(updateArray.slice(0, 50));
    updateArray = updateArray.slice(50);
}