I'm fairly new to JS and I'd love feedback if this script is the most effective. I'm trying to link records between tables, and I know it's not a simple copy-paste.
I have two tables: enrollments (students) and courses.
Enrollments has the fields:
- Course ID: integer
- Course ID Link: link field to the Courses table
The integers directly correspond to a 'Canvas Course ID' in the Courses table. Manually, I can copy from one column to the next to make the link.
From what I've pieced together, you need to query to find the AT recordID for that matching integer. Here is how I'm approaching this:
let enrollTable = base.getTable('Enrollments');
let enrollList = await enrollTable.selectRecordsAsync({fields: ['Course ID']})
for (let student of enrollList.records) {
//find the matching Course from Courses table
let coursesTable = base.getTable('Courses');
let courseQuery = await coursesTable.selectRecordsAsync({fields: ['Canvas Course ID']});
let filteredTerms = courseQuery.records.filter((record) => {
let name = record.getCellValue('Canvas Course ID');
return name == student.getCellValue('Course ID')
});
//link matching Course record into Student record
for (let course of filteredTerms) {
await enrollTable.updateRecordAsync(student, {
'Course ID Link': [{id: course.id}]
})
}
}
This is a very slow script. It takes about 0.5 seconds per record, which isn't feasible when I have thousands of students.
Is there a different approach to this? Thank you.