
## Summarize linked records (automations only)

This script creates a summary of several linked records and outputs the summary.
You can use the summary in a later automation action however you like, for example by sending it in an email.

You need to configure a `recordId` input variable in your automation action.
This variable should refer to the ID of the record containing links you'd like to summarize, and could come from a 'record created' trigger or a 'record enters view' trigger etc.

```js

// the table containing record links, and the linked-to table
let rootTable = base.getTable("Projects");
let linkedTable = base.getTable("Tasks");

// the record with the links we want to summarize
// we need to create a 'recordId' input variable connected to a record trigger
let config = input.config();
let recordId = config.recordId;

// query the table and find our record according to its id:
let rootQuery = await rootTable.selectRecordsAsync({fields: ['Tasks']});
let record = rootQuery.getRecord(recordId);

// query for the linked records:
let linkedQuery = await linkedTable.selectRecordsAsync({fields: ['Status']});
let linkedRecords = (record.getCellValue("Tasks") || []).map((link) =>
    linkedQuery.getRecord(link.id)
);

// analyze linked records to produce summary:
let incompleteRecords = linkedRecords.filter(
    (linkedRecord) => linkedRecord.getCellValueAsString("Status") !== "Done"
);
let completedCount = linkedRecords.length - incompleteRecords.length;

let summary = `
${completedCount}/${linkedRecords.length} tasks completed!
Remaining tasks:
`;

for (let incomplete of incompleteRecords) {
    summary += `- ${incomplete.name} (${incomplete.getCellValueAsString(
        "Status"
    )})\n`;
}

console.log(summary);

// output the summary so it can be used in subsequent automation actions:
output.set("Summary", summary);

```
