Help

Re: Upload files in correct sequence to attachment

Solved
Jump to Solution
2662 6
cancel
Showing results for 
Search instead for 
Did you mean: 
John-Paul_Kerno
7 - App Architect
7 - App Architect

I am trying to upload several jpgs to one record. I use ‘Gallery’ and drop files in bulk to the plus sign bottom right. They are pages from a book and therefore in a sequence. The files are numbered as jpgs in a numerical sequence.

Why can I not have the viewed output remain in the correct sequence?
Why is the output always reversed with the last item first?
How can I fix this?

Many thanks if anybody can help!

40 Replies 40
John-Paul_Kerno
7 - App Architect
7 - App Architect

I can obviously order the files if one attachment is in one record but not with multi attachments in one record.

Well, we don’t know [exactly] how many threads are allowed either. Imagine it is two, or three, and further imagine that the first thread gets an HTTP route that traverses servers and routers that are slightly slower than the others of the same size. It’s a cesspool of seemingly random outcomes, and they might was well be randomly ordered if any of these ideas are actual truths.

Have you confirmed that one file at a time sustains the order you expect?

If your new array contains only the url of the attachment, then there is a new upload. However, if you keep the full object information for the existing attachment (which includes far more info than just the url), there is no new upload.

This little script will sort attachments for a single record in alphabetical order by filename. It could be easily adapted to work on all the records in a table/view. Note that this is a strictly alphabetical sort so a file named “10.jpg” would sort before “2.jpg”. A numeric sort based on filenames, would depend on the exact file name structure.


let table = await input.tableAsync("Select a table");
let field = await input.fieldAsync("Pick an attachment field", table);
let record = await input.recordAsync("Pick a record with attachments", table)
let recordId = record.id;

let attachmentData = record.getCellValue(field.name);
output.inspect(attachmentData);

if (attachmentData && attachmentData.length > 1) {
    let newAttachmentData = sortArray(attachmentData);
    await table.updateRecordAsync(recordId, {
        [field.name]: newAttachmentData,
    });
    let queryResult = await table.selectRecordsAsync();
    let updatedRecord = queryResult.getRecord(recordId);
    let updatedAttachmentData = updatedRecord.getCellValue(field.name);
    output.inspect(updatedAttachmentData);
}

function sortArray (attachmentArray) {
    return attachmentArray.sort((a,b) => {
        return a.filename.localeCompare(b.filename);
    })
}



kuovonne
18 - Pluto
18 - Pluto

And here’s the script that will sort the attachments in alphabetical order for all records in a view.

Note: Make sure that all uploads are done before running the script or you could get unexpected results.


let table = await input.tableAsync("Select a table");
let view = await input.viewAsync("Select a view", table);
let field = await input.fieldAsync("Pick an attachment field", table);

let queryResult = await view.selectRecordsAsync();
let records = queryResult.records;

for (let record of records) {
    let attachmentData = record.getCellValue(field.name);
    if (attachmentData && attachmentData.length > 1) {
        let newAttachmentData = sortArray(attachmentData);
        await table.updateRecordAsync(record.id, {
            [field.name]: newAttachmentData,
        });
    }
}


function sortArray (attachmentArray) {
    return attachmentArray.sort((a,b) => {
        return a.filename.localeCompare(b.filename);
    })
}


If this answers your question, please mark this post as the solution. Otherwise, could you please give a bit more details and a screen capture?

One at a time definitely does.

This is amazing … but frightens me as I don’t know how to run it! Sorry … any help appreciated.

@John-Paul_Kernot The script that I wrote goes into a Scripting Block.
You need to install Scripting Block, and then paste in the code into the Scripting Block editor. Then press the Run button.

I always recommend testing out a new script on a copy of your base.

Awesome!

I tried this out buy it won’t run. I tried the first script “sort attachments for a single record in alphabetical order by filename”.
I created a new base
I created a new gallery
I uploaded 51 attachments to one field all sequenced from “IMG-0214-APER-SM.JPG”, “IMG-0215-APER-SM” … etc
I ran the script.

There is a code error I can’t fix. Something to do with the Field “attachment”.

FYI- I notice that the filed is automatically created by Airtable as “Attachments” but I have tried that to no success. Looking at renaming the field in the Airtable rename field dropdown I notice that although it is called “Attachments” in the table header, it is referred to there as “Attachment”.

What to do?

Screen Shot 2020-06-17 at 12.11.07

Correct - the script is apparently looking for field named “attachment” (field names are case sensitive). Rename the “Attachment” field to “attachment” and run the script again.