Help

Re: Avoid script exceeding exec time limit

2018 0
cancel
Showing results for 
Search instead for 
Did you mean: 
SBNZ
4 - Data Explorer
4 - Data Explorer

I have an automation that runs a script, when a record enters a view.

When I run the script as a test (in the automation setup mode), there’s never a problem. However, if the automation triggers, the script is always stopped by execution time limit.

Any idea how to fix this?
It’s usually only a couple of records int he view that triggers, so it shouldn’t be heavy computing at all.

//Copy Values to a linked field

//Declare tables
var activeTbl = base.getTable("Delivered");
var activeView = activeTbl.getView("Createing Link to Prepared")
var linkTbl = base.getTable("Prepared");
var linkView = linkTbl.getView("Prep for PD");


//Declare queries
var activeQry = await activeView.selectRecordsAsync();
var linkQry = await linkView.selectRecordsAsync();

//Loop through your active table records
for(let activeRec of activeQry.records){

    //Declare field in the active table you'd like to match
    let actField = activeRec.getCellValueAsString("Referenz") //If a Number, use getCellValue instead of getCellValueAsString

    //Declare Variable that will represent your new value
    let updRec = undefined;

    //Loop through the records in the link table
    for(let linkRec of linkQry.records){
        
        //Declare field in the link table you want to match to
        let linkField = linkRec.getCellValueAsString("Name") //Whether getCellValue/getCellValueAsString  is used should be the same as above

        //Match the records by that field
        if(actField == linkField){
            updRec = linkRec.id;
            break;
        }
    }

    //Update Records
    if(updRec !== undefined){
        await activeTbl.updateRecordAsync(activeRec, {
            "Link to Prepared": [{id:updRec}] //The field type must be a linked field
        });
    }
}
3 Replies 3
Chris_Parker
6 - Interface Innovator
6 - Interface Innovator

Limiting which fields are returned might help. As it is, you’re returning all fields with .selectRecordsAsync(). If you do something like this you might be in better shape.

.selectRecordsAsync({
  fields: ["Name", "Description"]
});

You could also throw in a bunch of console.log(new Date()); to see where the time hogs are.

YMMV!

edit: By the way, more than one console.log(new Date()); will be near impossible to identify after the script has run. Put some sort of identifier in it like this:

// increment the 1 each time you add this line somewhere
console.log(`1: ${new Date()}`);

Here are a few things that stand out to me:

  • You have nested loops, which can be big time hogs. Instead, use two different loops without nesting. The first loop would make an object mapping the link record name to the record Id. The second loop would go through the active records and consult the object map to find the match.

  • Your script does not use the triggering record (if there is one).

  • You are updating one record at a time inside the loop. Instead, create an array of records to update, and then update them outside the loop in batches of 50. This is the number one change that will speed up your script.

  • You are asking Airtable for all of the fields in both views, but only using two fields. If you limit the fields returned to the ones you need it may speed things up.

By the way, how long is your script taking to run? I thought that the 1 second execution time limit was removed per a recent announcement. (But even if the time limit is no longer a constraint, making the script more efficient is good practice.)

SBNZ
4 - Data Explorer
4 - Data Explorer

Many thanks for your replies!

I will implement some of the recommendations and monitor the result.