Hi guys,
I'm relatively new to scripting, so I've mostly used scripts I find in this amazing community and adapted them to my tables. Once such script I've tweaked to create junction tables. It automatically creates entries in a junction table when a linked record field is updated. It has been working pretty well for a 5-6 months now, then all of a sudden earlier today, it started giving the error:
ERROR
TypeError: Cannot read properties of null (reading '0')
at main on line 38"
Usually I can figure out what is going on and fix it, but this really has me stumped. I didn't touch the script today (I'm the only one who manages them). I took a snapshot of the base from this morning and the script works in that, so I tried to copy/paste the code from that to the live version, but it still didn't work. Which lead me to believe that has something to do with a change in the base, like a changed field or table name.
So I thought, okay maybe someone accidentally changed a field name. Double checked everything like 26 times and all of the fields/table names look exactly the same. I was considering just migrating over to the snapshot from earlier today, but that is going to be a nightmare with sending out new links for forms and views to our afterschool instructors. Hoping someone can shed some insight on what is happening! Thanks in advance.
// @ts-nocheck
let projectsTable = base.getTable('Weekly Instructor Scheduling');
let projectQuery = await projectsTable.selectRecordsAsync({fields: ['Name', 'Subs (Pending)','Track Sub Pending']});
let projectRecords = projectQuery.records;
let companyTable = base.getTable('After School Instructors');
let companyQuery = await companyTable.selectRecordsAsync({fields: ['Name']});
let joinTable = base.getTable('Sub Accept/Reject Table');
let joinQuery = await joinTable.selectRecordsAsync({fields: joinTable.fields});
// Go through all the records in the class table
for (let projectRecord of projectRecords) {
// For each class, get the linked instructor records
let companyRecordsLinkedToProject = projectRecord.getCellValue('Subs (Pending)');
if (!companyRecordsLinkedToProject) {
continue;
}
// For each class, get the linked join records
let joinRecordsLinkedToProject = projectRecord.getCellValue('Track Sub Pending');
if (joinRecordsLinkedToProject === null) {
// If there are no linked join records, use an empty array instead
joinRecordsLinkedToProject = [];
}
// Create a Set of instructor record ids that are linked to the class.
// A Set is like an array, but more efficient.
let companyRecordIds = new Set();
for (let linkedCompanyRecord of companyRecordsLinkedToProject) {
companyRecordIds.add(linkedCompanyRecord.id);
}
// Loop over all join records linked to the class.
for (let linkedJoinRecord of joinRecordsLinkedToProject) {
// Get the full join record, not just name and id
let joinRecord = joinQuery.getRecord(linkedJoinRecord.id);
// ERROR HERE
let companyRecordLinkedToJoin = joinRecord.getCellValue('After School Instructors')[0];
// ERROR HERE
let companyRecordId = companyRecordLinkedToJoin.id;
// Remove this instructor record id from the Set.
// @ts-ignore
companyRecordIds.delete(companyRecordId);
}
// Now we have a Set of instructor record ids that don't have join records.
for (let companyRecordIdToJoin of Array.from(companyRecordIds)) {
// Create the join record linking this class and this instructor.
let companyRecord = companyQuery.getRecord(companyRecordIdToJoin);
let companyName = companyRecord.getCellValueAsString('Name');
await joinTable.createRecordAsync({
'Weekly Instructor Scheduling': [{id: projectRecord.id}],
'After School Instructors': [{id: companyRecordIdToJoin}],
});
}
}
// Get Join Records Now that the Script has Run
let joinQueryTwo = await joinTable.selectRecordsAsync({fields: joinTable.fields});
let joinRecordsTwo = joinQueryTwo.records;
let nonMatches = joinRecordsTwo.filter(c => !c.getCellValueAsString('All Subs Pending').split(', ').includes(c.getCellValueAsString('After School Instructors')))
// Delete Irrelevant Records Now that the Script has Run
await joinTable.deleteRecordsAsync(nonMatches)
The error is on this line:
let companyRecordLinkedToJoin = joinRecord.getCellValue('After School Instructors')[0];
Final notes: This is a script in an automation that triggers on changes to the linked record field "Subs (Pending)" in the "Weekly Instructor Scheduling" table