I’m working on a script that transfers student data from our system to Airtable through their API. The data transfer works fine, but I’m getting duplicate entries created in Airtable even though my payload doesn’t contain any duplicates when I check it.
Here’s my code that identifies which students need to be added:
for (let student of studentArray) {
if (student.enrollment_status === 'active' || student.enrollment_status === 'pending') {
const isNewStudent = !existingRecords.find(record => record.student_id === student.student_id);
if (isNewStudent) {
studentsToAdd.records.push({
fields: {
...student
},
});
}
}
sendToAirtable('appXYZ123','Enrollments', studentsToAdd)
}
Function that handles the API request:
function sendToAirtable(baseId, table, data) {
var requestOptions = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json',
"Accept": "application/json"
},
payload: JSON.stringify(data)
};
var result = UrlFetchApp.fetch('https://api.airtable.com/v0/' + baseId + "/" + encodeURIComponent(table), requestOptions).getContentText();
return result;
}
Any ideas why duplicates keep showing up?