Skip to main content
Solved

Data validation before using updateRecordAsync

  • September 23, 2021
  • 5 replies
  • 0 views

Forum|alt.badge.img

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!

Best answer by Kamille_Parks11

The_Focus_Forwa wrote:

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.


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.

View original
Did this topic help you find an answer to your question?

5 replies

Kamille_Parks11
Forum|alt.badge.img+25

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)
}

Forum|alt.badge.img
  • Author
  • Known Participant
  • 12 replies
  • September 23, 2021
Kamille_Parks11 wrote:

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)
}

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?


Kamille_Parks11
Forum|alt.badge.img+25
The_Focus_Forwa wrote:

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?


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)

Forum|alt.badge.img
  • Author
  • Known Participant
  • 12 replies
  • September 28, 2021
Kamille_Parks11 wrote:

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)

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.


Kamille_Parks11
Forum|alt.badge.img+25
The_Focus_Forwa wrote:

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.


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.


Reply