Image URL to Attachment Script

Hey there. I’m trying to set up some script to automatically input my Images URL to Attachment. I found some code in this forum but I’m running into errors that I don’t understand.

Code I’m running:

let table = base.getTable('PhantomBuster');

let query = await table.selectRecordsAsync();

for (let record of query.records) {
    // if the attachment field is empty
    if(record.getCellValue('Image URL') == null) {
        if(record.getCellValue('Attachment') == null) {
        let recordId = await table.updateRecordAsync(record, {
            'Attachment': [
                { url: record.getCellValue('Image URL')}
            ]
        }})
    }
}

The error I’m running to is:
SyntaxError: missing ) after argument list
on line 1
at s on line 1
at Generator._invoke on line 1
at Generator.next on line 1
at t on line 1
at a on line 1
on line 1
on line 1

Can anyone help? Thanks

Welcome to the Airtable community!

What is your scripting background? How comfortable are you writing and debugging code?

I think you have swapped the order of the characters here. It should probably be })}. But you also might want to fix your indenting to make this easier to see.

You are also probably going to run into problems with this line as well. You are testing if there is no url, not if there is one.

Hey there kuovonne, Thanks for your answer.

1 - I have close to none experience with coding and debugging ^^ I bet that is easy to confirm haha
2- OMG I feel dumb, Code is ok now! But what you mean with “fix my identing”?
3- I want the code to run ONLY in case there is Attachment and Image URL. Isn’t that right?

UPDATE
Took out the Image URL field and fixed the bracket. Running into other error.

Code: `let table = base.getTable(‘PhantomBuster’);

let query = await table.selectRecordsAsync();

for (let record of query.records) {
// if the attachment field is empty
if(record.getCellValue(‘Attachment’) == null) {
let recordId = await table.updateRecordAsync(record, {
‘Attachment’: [
{ url: record.getCellValue(‘Image URL’)}
]
})}

}`

Error:
j: Can’t set cell values: invalid cell value for field ‘Attachment’.
Cell value has invalid format: .0.0.url is null, .0.1.id is missing, .0.1.url is null, .0.1.filename is missing.
Attachment field value must be an array of objects. New attachments must have property ‘url’ and optional ‘filename’. Existing attachments must be passed back unmodified.
at main on line 8

Any idea?

What is your interest level in learning? (Versus just getting working code)

Code is normally indented to show how some lines are nested inside other code. For example, code nested inside a loop or nested under a condition. In JavaScript, indenting doesn’t affect the execution of the code, but it makes debugging much easier.

If you are interested in learning to code, having clear indenting is an important habit to build.

That’s what I would expect. But that is not what your first code sample does, which is why I pointed out that line. It also probably is related to the error you got when you changed the code.

If you are interested in learning to code, it is a very simple change to the logic of your if statement. I have pointed out the line. It is a very small mistake in that line that should be easy to figure out if you are interested in learning to code.