Help

Change the filename of attachment to new record

Topic Labels: Automations
1326 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Tim_Asklof
4 - Data Explorer
4 - Data Explorer

Hi,

I am currently working on a script where:

  1. The user uploads one or more documents in one table
  2. Once the document(s) is uploaded, it should be renamed to follow a set standard
  3. In a second table, each attachment should be created as a new line
  4. The attachment should have its new filename.

The automation is set up in four steps:

  1. It triggers if the attachment field is updated.
  2. A conditional action is triggered as long as the record was not last modified by automation (to avoid infinity loop)
  3. The first script runs and renames the file(s)
  4. The second script runs and creates one or more records for the uploaded attachment.

Everything works fine except that in step 4, the filename to the attachment is the old filename (the name the file had when being uploaded) and not the new one.

Here is a picture of the current setup:
Skärmavbild 2022-05-19 kl. 18.47.29

Here is the first script (step 3):

// Setup
let config = input.config()
let table = base.getTable("Suppliers")
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)
let oldName = config.oldName
// Collect record data
let newPrefix = record.getCellValue("Name")
let files = record.getCellValue("Attachments")

// Get the extension and add it to the new name
let newFiles = []
for (let file of files) {
    let parts = file.filename.split(".")
    let ext = parts[parts.length - 1]
    let fileNumber = "0" + (files.indexOf(file) + 1)
    let newName = `${newPrefix}-${fileNumber}.${ext}`
    if (file.filename != oldName) {
        newFiles.push({url: file.url, filename: newName})
    }
    
}

const updateTable = async function() {
    // Reattach the item with the new filename
    await table.updateRecordAsync(record, {
        "Attachments": newFiles
    })
    output.set("Test", newFiles)
    
}

updateTable()

Here is the second script (step 4):

const tableDest = base.getTable("Documents")
const attachments = tableDest.getField("Attachments")
const view = tableDest.getView("Grid view")
let inputconfig = input.config()
const urls = inputconfig.urls
const fileNames = inputconfig.fileName
console.log(urls)
console.log(fileNames)
console.log(attachments)


async function uploadDocuments() {
    const records = await view.selectRecordsAsync()
    // Array for records with attachments and their info
    let attachmentInfo = []
    for (let i=0; i < urls.length; i++) {
        let url = new URL(urls[i])
        let fileName = fileNames[i]
        console.log(fileName)
        let path = url.pathname.split("/")
        if(records.records.find((r) => {
                return r.getCellValueAsString(attachments.name).includes(path[path.length-1])
        })) {
            continue
        }

        attachmentInfo.push({
            fields: {
                [attachments.name]: [
                    {url: urls[i]},
                ]
            }
        })
        console.log("hej", attachmentInfo)
        console.log("då", attachments.name)
    }
    while (attachmentInfo.length > 0){
        await tableDest.createRecordsAsync(attachmentInfo.slice(0,50));
        attachmentInfo = attachmentInfo.slice(50)
    }
}

uploadDocuments()



What can I do to ensure that the file created in the second table with the new file also has the new filename?

1 Reply 1

Hi Tim, I would specify the file name again in the second script. It also already exists in the the for loop.