and failing, to be honest. Please take note that I have no idea what I’m doing and this script is pieced together from snippets and posts I found on the forum.
This is what i have until now. I’ve tried multiple variations but I haven’t been able to write to a new attachment field correctly.
//script settings
let settings = input.config({
title: 'Create individual records from multiple attachments in a single attachment field',
description: 'Creates 1 individual record in another table for each attachment in a multiple attachment field in the source table, and links the new records back to the source record.',
items: t
// Source table select
input.config.table('tableSource', {
label: 'Table with existing attachments'
}),
// Source table: Attachment field
input.config.field('attachField', {
parentTable: 'tableSource',
label: 'Attachment field with multiple attachments to split',
}),
// Source table: Selected View
input.config.view('selectedView', {
label: 'View from the selected table',
parentTable: 'tableSource',
}),
// Destination table select
input.config.table('tableDest', {
label: 'Table to create new records in'
}),
// Destination table: Name or title field
input.config.field('destinationField', {
parentTable: 'tableDest',
label: 'Deliverable name or title field in destination table',
}),
]
});
async function splitAttachments() {
let { tableSource, attachField, selectedView, tableDest, destinationField } = settings;
if (attachField.type !== 'multipleAttachments') {
output.text(`"${attachField.name}" is not an attachment field. Run the script again with an attachment field.`);
return;
}
// Loads the records and fields from the selections above
let attachQuery = await selectedView.selectRecordsAsync();
console.log('query:', attachQuery)
let attachRecords = attachQuery.records;
console.log('records', attachRecords)
// Loops through qualified records and create new records in target table
for (let i=0; i<attachRecords.length; i++){
let attachments = attachRecordsei].getCellValue(attachField);
console.log('attachments:', attachments)
if(attachments !== null){
// Array for records with attachments and their info
let attachmentInfo = n]
for(let l=0; l<attachments.length; l++){
attachmentInfo.push({
id: attachmentshl].id,
fields: {
destinationField]: e
{url: attachmentshl].url},
]
}
})
}
console.log('attachmentInfo', attachmentInfo)
while (attachmentInfo.length > 0){
await tableDest.createRecordsAsync(attachmentInfo.slice(0,50));
attachmentInfo = attachmentInfo.slice(50)
}
}
}
}
await splitAttachments();
I’ve gotten various errors ranging from needing to define fields to incorrectly defining fields. The error I get with this iteration is:
ERROR
P: Invalid record format. Please define field mappings using a `fields` key for each record definition object
at splitAttachments on line 62
at async main on line 68
Anybody know how to properly structure an attachment field array? Any help is appreciated!
Thanks