Help

Re: Error handling if single select field option doesn't exist

Solved
Jump to Solution
1302 0
cancel
Showing results for 
Search instead for 
Did you mean: 
The_Focus_Forwa
6 - Interface Innovator
6 - Interface Innovator

I’m running a scripting app that loops through all the records in my Airtable using await table.updateRecordAsync() in a few fields, including a single-select field. Every once in a while, through human error or something else, we’ll wind up passing something to the script that makes it try to update the single-select field with some value that isn’t one of the options for that field. Right now what that does is cause the script to throw an error and abort entirely. What I’d like to do is have it throw an error message and continue on to the next record in the table. What’s the best way to go about this?

Excerpt of the script in question below (for context, we’re a nonprofit helping folks in the criminal justice system):

if (parsedNewDate.valueOf() != parsedOldDate.valueOf() ||
                    postSentencePrison != newPrison||
                    inmate.releaseCode) {
                            if (parsedNewDate.valueOf() != parsedOldDate.valueOf()){                                    output.markdown(`*Updating release date of ${name} (or as BOP knows them, ${inmate.nameFirst} ${inmate.nameLast}) from ${projectedReleaseDate} to ${newDate}*`);
                                await table.updateRecordAsync(record, {
                                    "Release Date": parsedNewDate,
                                });
                            };
                            if (inmate.releaseCode) {                                    output.markdown(`Marking *${name} (or as BOP knows them, ${inmate.nameFirst} ${inmate.nameLast}) as released.*`);
                                await table.updateRecordAsync(record, {
                                    "Status": { name: "Released"},
                                });
                            continue;
                            };
                            if (postSentencePrison != newPrison){                                    output.markdown(`*Updating location of ${name} (or as BOP knows them, ${inmate.nameFirst} ${inmate.nameLast}) from ${postSentencePrison} to ${newPrison}*`)
                                await table.updateRecordAsync(record, {
                                    "Post-Sentence Prison": { name: newPrison},
                                });
1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

You can check if the option exists for the single-select field.

You can get the list of choices from the field.

const singleSelectField = table.getField("Name of Single Select Field")
const choiceNames = singleSelectField.options.choices.map(choice => choice.name)

You can then do different actions depending on whether or not the choice exists.

let myValue = "something"
if (choiceNames.includes(myValue)) {
  await table.updateRecordAsync(record, {
    [singleSelectField.name]: {name: myValue}
  }
} else {
  console.log(`Choice ${myValue} does not exist for field ${singleSelectField.name}`)
}

By the way, you indicate that you are updating records one at a time in a loop. As you get more comfortable with coding, you may want to consider reworking the script so that the loop creates an array of records to update, and then updates them in bulk outside of the loop. Updating records in bulk will speed up your script execution time.

See Solution in Thread

6 Replies 6

Hi @The_Focus_Forward_Pr - I think a then...catch approach should do what you want, something like:

await table.updateRecordAsync(a, {
        'Single select': {name: selectValue}
    })
    .then(() => {console.log('this worked')})
    .catch(err => console.log('this failed', err))   

A good article here explains it better than I can :slightly_smiling_face:

https://lucymarmitchell.medium.com/using-then-catch-finally-to-handle-errors-in-javascript-promises-...

kuovonne
18 - Pluto
18 - Pluto

You can check if the option exists for the single-select field.

You can get the list of choices from the field.

const singleSelectField = table.getField("Name of Single Select Field")
const choiceNames = singleSelectField.options.choices.map(choice => choice.name)

You can then do different actions depending on whether or not the choice exists.

let myValue = "something"
if (choiceNames.includes(myValue)) {
  await table.updateRecordAsync(record, {
    [singleSelectField.name]: {name: myValue}
  }
} else {
  console.log(`Choice ${myValue} does not exist for field ${singleSelectField.name}`)
}

By the way, you indicate that you are updating records one at a time in a loop. As you get more comfortable with coding, you may want to consider reworking the script so that the loop creates an array of records to update, and then updates them in bulk outside of the loop. Updating records in bulk will speed up your script execution time.

Thanks, I tried this, but I’m getting a type error:

Argument of type ‘string’ is not assignable to parameter of type '“Alderson FPC” | “Aliceville FCI” | “Allenwood Low FCI” |…

My constants are set up like so:

const knownPrisonsField = table.getField("Post-Sentence Prison");
const knownPrisons = knownPrisonsField.options.choices.map(choice => choice.name)

And the relevant part of what’s left is here:

                        if (postSentencePrison != newPrison){
                            if (knownPrisons.includes(newPrison)){
                                output.markdown(`*Updating location of ${name} from ${postSentencePrison} to ${newPrison}*`)
                                await table.updateRecordAsync(record, {
                                    "Post-Sentence Prison": { name: newPrison},
                                }
                            } else {
                                output.markdown(`${newPrison} not found in list of prisons. You may need to add it manually.`)
                            }
                            };

I can also include how I set vars like newPrison from the API and existing records if helpful

Thanks! Would I need to declare a Promise or something before putting something like this in, or is that implied? I’m not familiar with the promise-then-catch syntax

Can you post a screen shot of the issue? If you try to run the script, does it run?

Sometimes the editor puts squiggly red underlines on code that actually runs just fine.

Never mind. It wasn’t running but I tried erasing and re-writing it and it works fine now. Must’ve been a misplaced bracket or something somewhere. You’re right, that error tooltip is still present, but the script now seems to work. Thanks for your help!