May 19, 2022 09:49 AM
Hi,
I am currently working on a script where:
The automation is set up in four steps:
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:
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?
May 20, 2022 06:56 AM
Hi Tim, I would specify the file name again in the second script. It also already exists in the the for loop.