Help

Re: Image URL to Attachment Script

4167 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Miguel_Costa
4 - Data Explorer
4 - Data Explorer

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

6 Replies 6

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.

Miguel_Costa
4 - Data Explorer
4 - Data Explorer

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?

Miguel_Costa
4 - Data Explorer
4 - Data Explorer

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.

taha041
4 - Data Explorer
4 - Data Explorer

Is there a corrected version of this code? I am also very new to coding and for the time being just need working code

thegove
4 - Data Explorer
4 - Data Explorer

I came across this post and it put me on the right track.  Here is my working code that uses the URL in the column named image_url and attaches the image at the URL to the record in the column named image.

 

 

let table = base.getTable('Item');
let query = await table.selectRecordsAsync({fields: table.fields});
let x = 0
for (let record of query.records) {
    // if the attachment field is empty
    if(record.getCellValue('image') === null && 
       record.getCellValue('image_url') !== null) {
        console.log(++x, record.getCellValue("image_url"))
        await table.updateRecordAsync(record, {
            'image': [{ url: record.getCellValue('image_url')}]
        })
      //break
    }
}