I am from a company selling STEM supplies to schools. I have three tables:
- A “Material List” table with a list of materials we have in inventory
- An “Activity List” table with a list of our STEM activities. Each activity has a linked record field which is populated with linked records in the “Material List” table

-
- An “Activity Material” junction table with the quantities of each material for each activity listed.

Desired Outcome of the Script:
I’ve written a script that is intended to:
-
Retrieve an activity record from the "Activity List" table using a passed record ID, whenever the list of linked Material records is changed.
-
Get the linked materials for that activity.
-
Compare the linked materials with existing records in the "Activity Materials" table.
-
Delete outdated records of materials no longer linked to the activity.
-
Create new records for materials that are newly linked to the activity, including setting the
Activity
andMaterial
fields with appropriate record references.
Here’s the script:
let activitiesTable = base.getTable("Activity List");
let materialsTable = base.getTable("Material List");
let activityMaterialsTable = base.getTable("Activity Materials");
// Get the record ID passed from the automation trigger
let inputConfig = input.config();
let activityRecordId = inputConfig.activityRecord; // This should be the Record ID passed from the trigger
// Fetch the full record from the Activity List table
let activityRecord = await activitiesTable.selectRecordsAsync().then(result => {
return result.records.find(record => record.id === activityRecordId);
});
// Check if the activity record exists
if (!activityRecord) {
throw new Error("Activity record not found.");
}
// Fetch the linked materials from the Activity List record
let linkedMaterials = activityRecord.getCellValue("Material Needed");
// Check if linked materials exist
if (!linkedMaterials) {
throw new Error("No materials linked to the activity.");
}
// Fetch all existing activity materials for this activity
let activityMaterialsQuery = await activityMaterialsTable.selectRecordsAsync();
let existingActivityMaterials = activityMaterialsQuery.records.filter(record => record.getCellValue("Activity").0].id === activityRecord.id);
// Delete old records for materials no longer linked
for (let existingRecord of existingActivityMaterials) {
let materialId = existingRecord.getCellValue("Material").0].id;
if (!linkedMaterials.some(material => material.id === materialId)) {
// Delete the record if the material is no longer linked
await activityMaterialsTable.deleteRecordAsync(existingRecord.id);
}
}
// Add new records for materials that are newly linked
for (let materialRecord of linkedMaterials) {
let existingMaterial = existingActivityMaterials.find(record => record.getCellValue("Material").0].id === materialRecord.id);
if (!existingMaterial) {
// Create a new record if it doesn't already exist
await activityMaterialsTable.createRecordAsync({
"Activity": >{ id: activityRecord.id }],
"Material": >{ id: materialRecord.id }],
"Quantity": 1 // Set quantity or retrieve it from another field if needed
});
}
}
Issue:
-
In the script, line 47 is causing an error: “Type '{ id: string; }' is not assignable to type 'string'.”
-
Am I using the deprecated selectRecordsAsync function? Any troubleshooting tips here would be much appreciated...