Skip to main content

Hi everyone.


I am trying to convert a img to a url automatically every time we upload or attached an image to a table.


This is the code.



let table = base.getTable("Table1");

let selectedView = table.getView("Shows");

let selectedField = table.getField("Speakers");

let fieldToEdit = table.getField("Speakers URL");

async function convertAttachmentsToUrls() {

if (selectedField.type !== 'multipleAttachments') {

console.log("is not an attachment field. Run the script again with an attachment field");

}

// Loads the records and field from the selections above

let query = await selectedView.selectRecordsAsync({

fields: [selectedField]

});

let records = query.records;

// Array for records with attachments and their URLs

let attachmentURLs = [];

// Loops through qualified records and pushes them to the attachmentURLs array

for (let i = 0; i < records.length; i++) {

let recID = records[i].id;

let attachments = records[i].getCellValue(selectedField);

if (attachments !== null) {

let attachmentLength = attachments.length;

let attachmentURL = '';

for (let l = 0; l < attachmentLength; l++) {

attachmentURL += attachments[l].url + ', ';

}

attachmentURLs.push({

id: recID,

fields: {

[fieldToEdit]: attachmentURL.slice(0, -2),

},

});

}

};

while (attachmentURLs.length > 0) {

await table.updateRecordsAsync(attachmentURLs.slice(0, 50));

attachmentURLs = attachmentURLs.slice(50);

}

}

await convertAttachmentsToUrls();

// Output message to indicate when the script is done

console.log('Records have been updated with attachment URLs');

This is the error.



the trigger is when a record is updated.


Can you help me to solve this issue?

Be the first to reply!

Reply