I am using Node.js library to communicate with AirTable, official js library. I am having a problem with filterByFormula
. Sometimes when I try to find record from AirTable I don’t get results even though I am pretty sure that there should be some results. My logic is such that if I do not find the record in AirTable I create a new one. If I find it I will update the existing one. This logic leads to problem with same entries entered twice.
This is my query:
getReservationByID: async (externalID) {
let reservations = await calendarBase('Reservations').select({
view: 'Main View',
filterByFormula: `{External ID} = \"${externalID}\"`,
maxRecords: 1,
}).all();
return (reservations.length > 0) ? reservations[0] : null;
},
And this function is used like this:
let oldReservation = await getReservationByID(reservation.id);
if (!oldReservation) {
createNewReservation(reservation);
} else {
updateReservation(reservation);
}
As you can see, it is essential for me to get record from AirTable if it exists, but sometimes AirTable is not returning an existing record with given ID and then I get duplicate with same external ID. Am I doing something wrong or is there some issue that I am not aware of?
I have managed to solve the problem. I have removed the view from query and now everything is working. If no view is included, Airtable will search all records in the table which match your formula. If you specify the view, Airtable will limit its search to just that view and then find all records that match your formula. If you have a filter on the view that you put in select, in my case 'Main View', then you may be hiding certain records from your script.
My new query looks like this:
getReservationByID: async (externalID) {
const reservations = await calendarBase('Reservations').select({
filterByFormula: `{External ID} = \"${externalID}\"`,
maxRecords: 1,
}).all();
return (reservations.length > 0) ? reservations[0] : null;
},