Sep 23, 2021 01:47 PM
I’m having a little trouble with a script I’m using to update a date field in Airtable with data from an API. The API I’m using gives date/time info most of the time. Every once in a while it’ll output a string like “UNKNOWN,” which brings the whole thing to a grinding halt. I’d like my script to be able to skip trying to enter that in and move on to the next iteration in the loop.
I’m feeding the data it gives me to a Date() function to save it as a new variable, then I pass that variable to table.updateRecordAsync(). But when the data is a string that can’t be parsed into a date, I get an error saying that the Airtable can’t store that string into a date field. Which is totally accurate and fine, but it halts the whole script.
Is there a function/operator or something to check whether a variable/constant/piece of data is in a format that Airtable will accept for a specific kind of field? Thank you!
Solved! Go to Solution.
Sep 28, 2021 11:18 AM
It is. You would need to check if a “choice” exists for a single/multi select field, a record id exists in the specified table, if a collaborator has access to the base, etc. and the way to do that is a combination of if statements (or ternary operators) and try{} catch{}
. Airtable does not provide a specialized validation function.
Sep 23, 2021 01:54 PM
Have you tried standard javascript error handling through try/catch?
https://www.w3schools.com/js/js_errors.asp
try {
// whatever your update record function is
} catch (err) {
console.log(err)
}
Sep 23, 2021 02:23 PM
Well, that’s helpful for pinpointing the error, but I already know what the error is coming from, and it still halts the script. It’s possible I’m doing it wrong, I am definitely a beginner with JS.
I’ll give you more specific info, that might clarify my original question. I’m working for a nonprofit that helps people who are incarcerated. The API I’m using tracks their release date and prison location. Here’s the relevant chunk of my script (newDate and anything in the inmate object are data from the API, projectedReleaseDate and postSentencePrison are data from the Airtable):
let parsedNewDate = new Date(newDate);
let parsedOldDate = new Date(projectedReleaseDate);
let newPrison = `${inmate.faclName} ${inmate.faclType}`;
if (parsedNewDate.valueOf() != parsedOldDate.valueOf() ||
postSentencePrison != newPrison||
inmate.releaseCode) {
output.markdown(`New data found for register number:** ${registerNumber}**`);
I want, if newDate is not valid to be entered into an Airtable date column, to set parsedNewDate to the same value as parsedOldDate. In other words, if the date the API feeds the script isn’t useful, I’d rather not update it and keep the data I have in the database (probably a null value). Does that make more sense?
Sep 23, 2021 03:30 PM
If you’re just trying to check if a “date” is in fact a date, a quick search for that pulls up this popular thread from Stack Overflow.
Following that advice means you can do:
let newDate = 'Unknown'
let parsedNewDate = isNaN(Date.parse(newDate)) ? null : new Date(newDate)
Sep 28, 2021 11:03 AM
Thank you! This is a good workaround for my specific case, but I imagine someone else might still want to test data for validity in a specific column type in Airtable more generally. If that’s just redundant to a collection of vanilla JS functions (as it was in this case), then I guess no need to respond, but I imagine things like single-select fields, collaborator fields, or barcodes might be a little trickier.
Sep 28, 2021 11:18 AM
It is. You would need to check if a “choice” exists for a single/multi select field, a record id exists in the specified table, if a collaborator has access to the base, etc. and the way to do that is a combination of if statements (or ternary operators) and try{} catch{}
. Airtable does not provide a specialized validation function.