Help

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

Re: Changing file name script

Solved
Jump to Solution
2808 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Edward1976
6 - Interface Innovator
6 - Interface Innovator

I have a script that runs without any errors but doesn't actually do anything. However, if I go to Test Automation and select a record to test, then run it, it works (almost) as expected. Therefore pretty confused what's going wrong. Image files are automatically downloaded from our server into the mockUpFiles field within a table. File names are like this 63eb679887a459_13030203recqKJDnAJSRJOsSL.jpeg. The script should use title + "_" + designTypeName + "_" + channel to change the filename to something which allows me to identify the contents. Can anyone help tweak this code to make it change the file name as soon as a record is created (Trigger is obviously set to run when a record is created)?

 

const imageAttachment = base.getTable("Pre Mock Prod (PB)");

const recordid = input.config()["recordid"];

const recordToUpdate = await imageAttachment.selectRecordAsync(recordid);

let title = recordToUpdate?.getCellValue("mockup_id");
let designType = recordToUpdate?.getCellValue("designType");
let channel = recordToUpdate?.getCellValue("channel");
let imageField = recordToUpdate?.getCellValue("mockUpFiles");
let newImageField = [];

if (imageField) {
  for (let [imageNumber, image] of imageField.entries()) {
    console.log(image, imageNumber);
  
    let findExtension = image.filename.match(/\w+_\d+\.\w+$/) || [""];
    let extension = findExtension[0];
    console.log(extension);

    let fileSpecificName = imageNumber > 0 ? "-" + (imageNumber) : "";
    fileSpecificName += designType;
    let designTypeName = designType ? designType.name : "";

    newImageField.push({
      url: image.url,
      filename: title + "_" + designTypeName + "_" + channel
    });
  }  
  console.log("New Image Field Array", newImageField);

  await imageAttachment.updateRecordsAsync([{
    id: recordid,
    fields: {
      mockUpFiles: newImageField
    }
  }]);
}

 

1 Solution

Accepted Solutions

Hi,

you should give first variable the same name, as it written in code. so, change 'recordID' to 'id' on the left side.

also, you are put all 4 values into the same variable, while you need to create separate variable for each

Alexey_Gusev_0-1676461597175.png

also check the right panel, after script run it should display input parameters like this (of course values are random data):

Alexey_Gusev_2-1676462367386.png

 


 
 

and by way, it doesn't matter how you will call them and how many - they all (except id) assigned to a variable rest and then joined into a string with given delimiter.

See Solution in Thread

4 Replies 4
Alexey_Gusev
12 - Earth
12 - Earth

feels like a bit overengineering.

you should acquire title, designTypeNamec, channel' in input parameters together with id,
so you have 4 variables on the left side, from Automation code editor.
I had to rewrite script. ternary operator on ix should have been easier, via + , but it didn't worked when testing, Array join solved it, but some simple way should exist here.

 

 

 

const table=base.getTable('Pre Mock Prod (PB)')
const {id,...rest}=input.config()
const files=await table.selectRecordAsync(id).then(rec=>rec?.getCellValue('mockUpFiles'))
const names=Object.values(rest).filter(n=>n).join('_')
const newnames=files?.map((f,ix)=>(Object.assign({},f,{filename:[names,ix? '_'+ix:''].join('')})))
if (files) await table.updateRecordAsync(id,{'mockUpFiles':newnames})

And by the way, your main problem is that files are being processed few seconds after upload, so your automation works before filename created and results then overwritten. So you can't see it on upload, but it looks good on test.

How is your record being created? Is it being created with the attachment, or is the record being created and then the attachment uploaded soon after? If the record is being created before the attachment is uploaded, you script does not do anything because there is no attachment yet. Even if your record is created with the attachment, it still takes several seconds for the upload to become fully attached.

There are many ways of doing things with code. Use a way that works and that you understand and can maintain.

You may also want to consider uploading the original file to one attachment field and then put the renamed attachment in a different field. Sometimes Airtable has trouble fully uploading attachments and if you do an in-place swap, and the file does not fully attach, you have loose the attachment completely. (Yes, this happened to me. I was renaming attachments at a time when Airtable attachments were momentarily unavailable and lost all the attachments. I had to go back to the source files and re-upload the originals.) 

Hey Alexey, thanks so much for taking the time to rewrite this. However, I am getting an error when I run this - I'm sure I am missing something obvious...this is the error:

TypeError: Invalid arguments passed to table.selectRecordAsync(recordId, options):
• recordId should be a string, not undefined
    at main on line 3

Hi,

you should give first variable the same name, as it written in code. so, change 'recordID' to 'id' on the left side.

also, you are put all 4 values into the same variable, while you need to create separate variable for each

Alexey_Gusev_0-1676461597175.png

also check the right panel, after script run it should display input parameters like this (of course values are random data):

Alexey_Gusev_2-1676462367386.png

 


 
 

and by way, it doesn't matter how you will call them and how many - they all (except id) assigned to a variable rest and then joined into a string with given delimiter.