Skip to main content

Here's something I created using automations with a script to allow me to get quick totals on any table view for specific values. I have 2 cases listed below and have found it to be a great way of collecting totals without having to create other linked records, rollups etc. Of course I have to hit the recalculate checkbox to get my total for each row, so it's not live, but I can live with that.  Also, you have to make sure your references are correctly spelled (Source Table, Source View, etc).  I hope others find it useful. I've attached the javascript below.

let inputConfig = input.config();

let record_id = inputConfig['SummaryRecID'];

// Reference the "Summary Table"
let summaryTable = base.getTable("Summary Table");

// Fetch the specific record in "Summary Table"
let summaryRecord = await summaryTable.selectRecordAsync(record_id, {
fields: ["Source Table", "Source View", "Field", "Value"]
});

if (!summaryRecord) {
console.log("Record not found in Summary Table.");
return;
}

// Load field values from the "Summary Table"
let sourceTblName = summaryRecord.getCellValue("Source Table");
let sourceVwName = summaryRecord.getCellValue("Source View");
let sourceFldName = summaryRecord.getCellValue("Field");
let sourceVal = summaryRecord.getCellValue("Value");

// Ensure all required fields exist
if (!sourceTblName || !sourceVwName || !sourceFldName || sourceVal === null) {
console.log("Missing required field values in Summary Table.");
return;
}

// Convert `sourceVal` to string in case it's a Single Select
if (typeof sourceVal === "object" && sourceVal !== null && "name" in sourceVal) {
sourceVal = sourceVal.name;
}

// Get the table object dynamically
let sourceTable = base.getTable(sourceTblName);

// Get the view object dynamically
let sourceView = sourceTable.getView(sourceVwName);

// Fetch records from the specified view
let sourceQuery = await sourceView.selectRecordsAsync({ fields: [sourceFldName] });

let count = 0;

// Loop through records in the source view and count matches
for (let record of sourceQuery.records) {
let fieldValue = record.getCellValue(sourceFldName);

// Handle Single Select fields (extract name)
if (typeof fieldValue === "object" && fieldValue !== null && "name" in fieldValue) {
fieldValue = fieldValue.name;
}

// Compare values (ensuring type safety)
if (fieldValue !== null && fieldValue.toString() === sourceVal.toString()) {
count++;
}
}

// Update the "Count" field in "Summary Table" for the given record_id
await summaryTable.updateRecordAsync(record_id, {
"Count": count
});

console.log(`Updated count to ${count} for record ${record_id}`);

 

Cool!

Best,
Milan - automable.ai
Free Airtable consultation


Reply