I’m trying out the new scripting block to try and make a utility for our base that will delete records based on a field and then delete the field itself. Is this possible?
Use case:
We use a table for semester data and then link it to all of the management tables. Each semester we make a new table and have to archive and update the linked field in all of the management tables. Semester data overlaps, so we can’t use the same table.
We audit our fields and sometimes find that we no longer use the field and we want to archive all old records that used that field.
Also, is it possible to export the filtered results to a CSV from a script? I would love to have this as part of the flow.
I’m a beginner at code but, here is my attempt from looking at a bunch of examples:
//Script to delete all records associated with a specific field and then delete the field.
//6-3-2020
output.markdown('# Batch Delete Records and Field');
output.markdown('#### Before use, download CSV of sorted records');
// get the table and field
let sourceTable = await input.tableAsync("Pick the table:");
let sourceTableName = sourceTable.name;
let sourceField = await input.fieldAsync("Pick the field to sort by:", sourceTable.id);
let sourceFieldName = sourceField.name;
// get records and sort for field
let query = await sourceTable.selectRecordsAsync();
let filteredRecords = query.records.filter( record => {
let recsToArchive = record.getCellValue(sourceFieldName);
return recsToArchive !== null
});
let recordCount = filteredRecords.length;
// show list of records to archive
output.markdown("### Selected Records for Deletion");
output.table(filteredRecords);
// Ask to delete Records
let deleteRecs = await input.buttonsAsync('Delete ' + recordCount + ' records?', d
{label: 'Yes', variant: 'danger'},
{label: 'Cancel'},
]);
// Delete records
if (deleteRecs === 'Yes'){
output.markdown('### Deleteing files...');
await sourceTable.deleteRecordsAsync(filteredRecords);
output.markdown(recordCount + " records deleted");
}
// Ask to delete Field
let deleteFields = await input.buttonsAsync( "Delete " + sourceField.name + "?",
{label: 'Yes', variant: 'danger'},
{label: 'Cancel'},
]);
// Delete Field
if (deleteFields === 'Yes'){
output.markdown('### Deleting Field...');
;
}