Help

Re: Script exceeded max CPU time

1498 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Florian_Verdon2
7 - App Architect
7 - App Architect

Hello everyone,

About one week ago, we started seeing more and more of this “Script exceeded max CPU time” error.
I looked at the code and don’t see “major performance flaws”. (but I am not an expert with JS and I might be doing something wrong without noticing)

I looked at @Bill.French race condition suggestion but cannot see one here.
I also checked that I wasn’t doing a “single update” multiple times. (@kuovonne recommendation on another thread)

Can anyone help me find some ways to optimize this script ?

let config = input.config();

let triggeredRecordId = config.RECORD_ID;
let candidateEmail = config.EMAIL;
let jobId = config.JOB[0];

let applicationsTable = await base.getTable("Applications");

let query = await applicationsTable.selectRecordsAsync();
let records = query.records;

let recordsWithSameEmailAndJob =
    records
    .filter(
        filterOlderApplications
    );

function filterOlderApplications(a) {
    let currentRecEmail = a.getCellValueAsString("Email");
    let currentJobs = a.getCellValue("Job");
    if (currentJobs === null || currentJobs === undefined || currentJobs.length === 0) return false;
    let currentRecJob = currentJobs[0].id;
    let currentRecId = a.id;

    let isSameApplication =  currentRecEmail === candidateEmail
                        && currentRecJob === jobId
                        && currentRecId !== triggeredRecordId;

    if (isSameApplication) console.log(isSameApplication);

    return isSameApplication;
}

let recordsUpdate = recordsWithSameEmailAndJob.map(r => {
    return {
        "id": r.id,
        "fields": {
            "Is not last version": true
        }
    }
});

let updated = await applicationsTable.updateRecordsAsync(recordsUpdate);

Thank you !

Florian

3 Replies 3

It’s been awhile since you posted, and I hope you have gotten your code to work. However, if you still need help, here are some thoughts …

Are you sure that the input variable JOB is an array?
If you do a console.log(jobId) does it show what you expect?


This line looks strange. You do not need to await the base.getTable command. You want
let applicationsTable = base.getTable("Applications");


One way to speed up scripts is to indicate what field values you want in selectRecordsAsync(). Returning only the fields you need can make for faster execution, especially if there are many fields. However, I suspect that this is not the root of your problem.


This line also looks strange. Later on you declare a function named filterOlderApplications. However, here you are not actually calling the function, and you are not passing the record to the filterOlderApplications function.

I suspect you want something more like

.filter(record => filterOlderApplications(record))

Have you check if your filter is actually working as expected? Or is it not actually filtering out anything?


There are a few places where your coding style is a bit wordy, and things could be tightened up, but none of those places should be causing a major performance hit.

Hello @kuovonne,

Thank you for taking the time to read my post and suggest some optimizations.

Actually, the code was working on some records but failing sometimes.

Indeed the script might be shortened and refactored a bit for elegance but… as you said, nothing that should be causing a major performance hit.

Our automations started working 100% again on Monday of this week and I think an update was release on Airtable because the “View execution log” panel doesn’t show the CPU limit anymore.
I don’t know if that limit was removed or increased but now it’s working fine… Mystery :grinning_face_with_big_eyes:

About the filter method on a list, I am pretty sure you can pass a callback function as reference that should accept on parameter (the current item that is being iterated) and return a boolean value. (should be kept in the list or not). (Reference : Array.prototype.filter() - JavaScript | MDN)

Thank you for your help !

Glad to hear your script is working now.

Yup. You’re right. I am so used to writing anonymous functions that I forgot that they do not need to be anonymous. Thanks for the reminder.