Script exceeded execution time when trying to retrieve a record by ID

Hi there community members

I’m still new to airtable scripting and really need your help in solving the timeout problem on one of my scripts.

I’m getting a script exceeded execution time on the following code. What can I do or how can I change the script to prevent it from happening

console.log(“Start Ditribution script”);

let inputConfig = input.config();
let leadRecordId = inputConfig.recordID;
let tableLeads = base.getTable(“LGA Leads”);

let query = await tableLeads.selectRecordsAsync({ fields: tableLeads.fields });
let leadRecord = query.getRecord(leadRecordId);

console.log(leadRecord.getCellValue(“Lead Key”));

I see the 1st console.log, but not the 2nd, so I assume it must be happening when I’m opening the table. The table has a lot of records.

Hey there!

My hunch is your script is timing out because you’re current query is actually pulling all records from the table.

I’d recommend using selectRecordAsync instead (note the singular “Record” vs. “Records” in selectRecordsAsync) which lets you pass a single recordId into it, to get to that record faster.

Hope that helps!

4 Likes

Thank you for you help

Can you let me know how I pass the id with the selectRecordAsync ? I need to retrieve all the fields to use it further on

let query = await tableLeads.selectRecordAsync({ fields: tableLeads.fields });
let leadRecord = query.getRecord(leadRecordId);

I had a look in the documentation, but it is not very clear

How many records and how many fields?
Is this the entire script, or is there more to the script?

While querying for just one record instead of all the records will improve performance some, I find it surprising that simply querying the table by itself will take over 30 seconds, even though you are getting all the data in all the records.

Learning to read documentation is a skill in and of itself. At first glance, it can look like a foreign language, but once you get used to it, there is a wealth of information in the documentation.

Try this …

console.log({leadRecordId})
let leadRecord = await tableLeads.selectRecordAsync(leadRecordId, { fields: tableLeads.fields });
2 Likes

Thanks I’ve added it and will check to see if that solves my problem.
Have a great day

I’ve seen a few enterprise bases exceeding the 50k record cap by a few thousand, and not even their 30k-record tables took anywhere near as long to fully load. Maybe if they are close to the 500-field limit? :sweat_smile:

1 Like

You are assuming all things equal, and they rarely are. How busy is the instance? How many API events? How many users? How many automations? The list of possible ways that comparisons are invalid is endless.

A sure way to bring Airtable to its knees is to request all records of a table without scoping the fields to only that which is needed.

Hi there

I changed the script to use selectRecordAsync (without the s and to select only the one record) and it works 100%. THANK YOU SO MUCH @kuovonne !

I do get a warning/error in the script, where the query result is underlined in red and the message is “Object is possibly 'null”. In reality, it should never be null because it is the record that triggers the update. But what is the best practice in this case? How do I deal with potentially null objects?

There is a chance that the record is deleted after the automation is triggered but before the code runs. In that case you can’t do much.

You can either structure you code to end if the record is null, or you can ignore the error since it is unlikely to happen anyway. I find it mostly a matter of aesthetics. I don’t like seeing the red underline and I don’t like getting errors about failed automations, so I use guard clauses to end my code if the object is null.

Thanks @kuovonne I’ll ignore it for now, the changes are zero for the record to be deleted before the code runs.

Can you please send me a link on how to use the guard clauses for future reference?

https://google.com/search?q=guard+clause+programming

You are a STAR !! Thanks @kuovonne

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.