We are moving student metrics to Airtable and I am trying to create a backlog of attendance records for our students since they started. (we have two campus's with seperate start dates). I have an automation working fine for populating records with every new day but what i need help with is the back log script. Currently, I am trying to create a record for every student for everyday they have attended class thus far.
Can someone help debug why this is not working? Nothing happens when I run the script and I do not get an error in the console, meaning the script is killed?
let studentTable = base.getTable("Participant Core Database");
let attendanceTable = base.getTable("Attendance");
let inTraining = studentTable.getView("Training View")
let currentStudents = await inTraining.selectRecordsAsync({
fields: ["Full Name", "Training Facility"],
sorts: [
{field: "Training Facility"}]
})
//gets an array of all the dates the student has attended class since they started (before airtable)
/**
* @param {string} s
* @param {string} e
*/
function getDaysArray(s,e) {
for(var arr=[], date = new Date(s); date <= new Date(e); date.setDate(date.getDate()+1)){
const day = date.getDay()
// only adds weekdays
day !== 0 && day !== 6 && arr.push(new Date(date).toLocaleDateString());
}
return arr
}
currentStudents.records.map(async (record) => {
const campus = record.getCellValueAsString("Training Facility")
let newDates = []
//each campus has different start dates
campus === "ASFC" ? newDates = getDaysArray("2023-02-07", "2023-03-16") : newDates = getDaysArray("2023-01-16", "2023-03-16")
newDates.map(async (date) => {
try {
await attendanceTable.createRecordAsync(
{
"Attendance Date": date,
"Student": [{id: record.id}],
"Attended": [{name: "Attended"}],
"Campus": [{ name: campus}]
})
} catch (Error) {
console.error(Error.message)
}
})
})