Apr 13, 2021 04:48 AM
I made this script which allows you to compare two lists based off a field value, then modify a checkbox when matches are found.
My use case example:
In this case the script works as such:
You can use this script to do anything however, essentially it compares two views, and where there is matches modifies a checkbox to be ticked or unticked.
CODE BELOW
//Header
output.markdown("### Compare Lists and Set Value");
// get the table name
output.markdown("### Select the Table");
let sourceTable = await input.tableAsync("Pick the table:");
let sourceTableName = sourceTable.name;
// prompt the user to pick a view:
output.markdown("### View to Reference");
let sublistView = await input.viewAsync("Select the view to reference", sourceTable);
let sublistViewName = sublistView.name;
let sublist = sourceTable.getView(sublistViewName);
let sublistRec = await sublist.selectRecordsAsync();
// prompt the user to pick a view:
output.markdown("### View to Modify");
let sourceView = await input.viewAsync("Pick a view to modify", sourceTable);
let sourceViewName = sourceView.name;
let sourcelist = sourceTable.getView(sourceViewName);
let sourcelistRec = await sourcelist.selectRecordsAsync();
// get the source field name
output.markdown("### Match Field");
let sourceField = await input.fieldAsync("Pick which field to reference:", sourceTable.id);
let sourceFieldName = sourceField.name;
//choose checkbox to modify
output.markdown("### Field to Modify");
let modifyField = await input.fieldAsync("Pick which field to modify:", sourceTable.id);
let modifyFieldName = modifyField.name;
//choose cell value
output.markdown("### Set Cell Value");
let setcell = await input.buttonsAsync(
'Set the cell value',
[
{label: '✅ ', value: true},
{label: ' ', value: false}
],
);
//build the sublist array to match sourcelist against
let subArray = [];
for (let record of sublistRec.records) {
subArray.push(record.getCellValueAsString(sourceFieldName));
}
//compare sublist to sourcelist and preform action on sourcelist
for (let record of sourcelistRec.records) {
if (subArray.includes(record.getCellValueAsString(sourceFieldName))) {
await sourceTable.updateRecordAsync(record, {
[modifyFieldName]: setcell,});
}
}
Apr 19, 2021 09:51 PM
Welcome to the community, @Nikita_Miltiadou! :grinning_face_with_big_eyes: Thanks for sharing your script! I moved the post to the “Show and Tell” category, as that’s a more appropriate place for sharing solutions with the community.