Hi guys,
I am just starting with the scripting and I am really trying my best. I have learned a lot just by trying out different submissions from the community and by doing that I understand how a script is structured etc.
I am currently focused on 3 specific scripts that was contributed to this community.
The first one is automatically setting up a field from a table to link all records from a view based on another table. It worked.
let mstTable = base.getTable('$MST'); let mstView = base.getTable('$MST').getView('TEST'); let mstQuery = await mstView.selectRecordsAsync(); let mstRecords = mstQuery.records; let invsumTable = base.getTable('$INV: SUM'); let invsumQuery = await invsumTable.selectRecordsAsync(); let invsumRecords = invsumQuery.records; let d = [] invsumRecords.forEach(c => d.push({id: [c.id](http://c.id/)})); let updateRecords = mstRecords.map(c=> ({id:c.id,fields:{'$INV: SUM': d}})); while (updateRecords.length > 0) { await mstTable.updateRecordsAsync(updateRecords.slice(0,50)); updateRecords = updateRecords.slice(50); } output.markdown('# Done
')
I encountered an error when linking records above 50. After searching and trying to understand the limit and how to control it, I added these to the code and it is working fine now.
await mstTable.updateRecordsAsync(updateRecords.slice(0,50)); updateRecords = updateRecords.slice(50); }
Extracting an image from a URL and attaching it to an attachment field.
// Change the names of this table/fields according to your base.
let submissionsTable = base.getTable('$MST');
let urlField = submissionsTable.getField('URL: QR Code | 64px');
let attachmentField = submissionsTable.getField('ATH: QR Code | 64px');
let submissionsQuery = await submissionsTable.selectRecordsAsync();
let updates = [];
for (let record of submissionsQuery.records) {
let url = record.getCellValue(urlField);
let attachments = record.getCellValue(attachmentField);
// If this record already has an attachment, skip it.
if (url === null || attachments !== null) {
continue;
}
// Otherwise, attach the image at the URL.
updates.push({
id: record.id,
fields: {
[attachmentField.id]: [{url: url}]
}
});
}
// Update records in batches of 50.
while (updates.length > 0) {
await submissionsTable.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50);
}
This is one of the first few scripts that I encountered here on the community and I noticed when reviewing my saved snippets that there was the while (updates.length > 0) lines included already so I searched airtable for the documentation and I was studying it as well. This is working fine as well with more than 50 records.
- Now this is where I am really stuck and I have been trying to understand this for weeks now. Renaming the attached file based on the file name output that I want for the attached file. Since this is also attached to an automation rule, there are variables already declared prior to the script.
let config = input.config()
let table = base.getTable("$MST")
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)
// Collect record data
let newPrefix = record.getCellValue("MST_CODE")
let newExtension = record.getCellValue("ATH: Calling Card | File Name")
let files = record.getCellValue("ATH: QR Code | 512px")
// Get the extension and add it to the new name
let newFiles = []
for (let file of files) {
let fileNumber = "0" + (files.indexOf(file) + 1)
let newName = `${newPrefix}_${fileNumber}${newExtension}`
newFiles.push({url: file.url, filename: newName})
}
// Reattach the item with the new filename
await table.updateRecordAsync(record, {
"ATH: QR Code | 512px": newFiles
})
^^ I have been trying to add the while update slice 50 lines and I cant seem to make it work.
I have also studied and experimented with Jeremys post (https://community.airtable.com/t/scripting-block-a-hopefully-helpful-batching-function/27757). I am thankful for this that I am able to understand it but I am just assuming that the code for Script #3 is structured differently thats why I cant seem to include the while update records slice command that I keep on seeing from different posts.
Can anyone please help me so that I can add the while slice code to the Script #3 to prevent errors? I have tried to modify this multiple times already. I just cant seem to make it work.
I would appreciate any assistance guys! And also, credits to the original contributors of these snippets. I am very thankful. I am very eager to understand the process as well so that I can understand it even more.