Jul 16, 2022 04:40 PM
Hello airtable community,
i need help about the bulk import script to split attachments.
the default script work as i want, but i need to customized it a bit.
Here is what i am trying to do, i want to split multiple attachment into a single row in new table and want to get the a certain field for source table to the new table in target field.
example: i have users upload multiple attachments using airtable form and from that table i will split the attachments into single row in new table which is working as intended now.
but i also want to take a certain field (department field) to move to the new table with matching attachments.
let settings = input.config({
title: '📎⇣ Bulk attachment importer',
description: 'Upload multiple attachments to a single attachment cell, then run the script to create 1 record for each attachment in another table',
items: [
input.config.table('sourceTable', {
label: 'Table with the uploaded attachments',
description: 'You can also add a button field to this table to start this script!',
}),
input.config.field('sourceField', {
label: 'Attachment field inside the above table',
parentTable: 'sourceTable',
}),
input.config.field('deptField', {
label: 'Dept field inside the above table',
parentTable: 'sourceTable',
}),
input.config.table('destinationTable', {
label: 'Table to create records with individual attachments',
}),
input.config.field('nameField', {
label: 'Name field inside the above table (where filenames will be entered)',
parentTable: 'destinationTable',
}),
input.config.field('attachmentField', {
label: 'Attachment field inside the above table where individual files will be uploaded',
parentTable: 'destinationTable',
}),
> input.config.field('targetdeptField', {
> label: 'Dept field where source dept name will be entered',
> parentTable: 'destinationTable',
}),
]
});
// Converts settings selections to variables
let sourceTable = settings.sourceTable;
let selectedField = settings.sourceField;
let deptField = settings.deptField;
let destinationTable = settings.destinationTable;
let nameField = settings.nameField;
let attachmentField = settings.attachmentField;
let targetdeptField = settings.deptField;
if (attachmentField.type !== 'multipleAttachments' || selectedField.type !== 'multipleAttachments') {
throw new Error('Selected field must be an attachment field')
}
let record = await input.recordAsync('Select a record with the attachments you wish to import', sourceTable); // Button field to run script or prompt user to specify the record
while (!record) {
output.text('You must select a record.');
record = await input.recordAsync('Select a record with the attachments you wish to import', sourceTable);
}
output.clear();
let attachments = record.getCellValue(selectedField); // Attachments in the above record
output.text("Available attachments: "); // Surface attachments for inspection
output.table(attachments)
if (attachments) {
// Creates an array based on the attachments to then create the individual records
let attachmentRecords = attachments.map(a => ({
fields: {
[nameField.id]: a.filename,
[attachmentField.id]: [{ url: a.url }]
}
}));
// Batches the record creation
while (attachmentRecords.length > 0) {
await destinationTable.createRecordsAsync(attachmentRecords.slice(0, 50));
attachmentRecords = attachmentRecords.slice(50);
};
output.markdown(`**${attachments.length} record(s) created!**`)
} else {
output.clear();
output.markdown('No attachments uploaded to this record. Upload attachments to the record and rerun the script.');
}
Jul 16, 2022 11:33 PM
Welcome to the community, @erick_osmanto! :grinning_face_with_big_eyes: You’ve already got a post on this topic over here:
Please don’t post a message on the same topic to more than one category. Enough people read the forums that someone will see your message regardless of which category it’s in. I recommend deleting this topic (if you can) to avoid confusion between the two threads.
As to your question, I don’t have time to dig into this now, but I’ve bookmarked your other post for review next week (the soonest I can get to it).
Jul 20, 2022 09:09 PM
Okay, it looks like the changes needed to make the department carry over will be pretty easy. First off, this line is written incorrectly:
let targetdeptField = settings.deptField;
You’re already capturing the deptField
value from the settings into another variable. This one should read:
let targetdeptField = settings.targetDeptField;
The other change is to this section:
let attachmentRecords = attachments.map(a => ({
fields: {
[nameField.id]: a.filename,
[attachmentField.id]: [{ url: a.url }]
}
}));
To add the department value to the new record based on the value from the source record, it should look like this:
let attachmentRecords = attachments.map(a => ({
fields: {
[nameField.id]: a.filename,
[attachmentField.id]: [{ url: a.url }],
[targetDeptField.id]: record.getCellValue(deptField)
}
}));
If your source and destination department fields are both text fields, this should work fine. If they’re single-select fields, this might not work (long story). If that’s the case and it throws an error, you might try this variant:
let attachmentRecords = attachments.map(a => ({
fields: {
[nameField.id]: a.filename,
[attachmentField.id]: [{ url: a.url }],
[targetDeptField.id]: {name: record.getCellValue(deptField).name}
}
}));
Jul 21, 2022 07:11 PM
Thank you @Justin_Barrett for the reply, i got this error when i run the script:
I am using linked record both in source table and target table
j: Can’t create records: invalid cell value for field ‘Target Dept’.
Cell value has invalid format: .0.0 must be an object.
Linked records field value must be an array of objects with property ‘id’ corresponding to linked record id.
at main on line 65
Jul 21, 2022 07:22 PM
Thanks for the clarification. Based on that description, I’m not sure why you’re getting the error that you listed. If the source Department field is empty, that emptiness should pass to the target field without any issues. If the source field is linked to one or more records in another table, that array of links should also work in the target field to point to the same records.
Would you be able to share a screenshot of the two tables in question, showing the Department field in both cases? A look at the table that these fields are linking to would also be helpful.