Scriping exceeding 1000 miliseconds sometimes

I created a script to be used into an automation when a new record is created. 90% of the times it runs well (using less than 500 miliseconds), no problem… but 10% it exceeds 1000 miliseconds of CPU time (I know about the restriction).

Is there a way to get a higher limit (even paying more)? Also I’m looking to try it more faster, which parts are “heavy time consumers” of my code for an Airtable script?

let users = base.getTable('CoreVette');
let reps = base.getTable('Reps');
let refs = base.getTable('References');

let inputConfig = input.config();
let newRecordId = inputConfig["recordId"];

let repsResult = await reps.selectRecordsAsync();
let repsStorage = {};
for (let recordRep of repsResult.records) {
    repsStorage[recordRep.getCellValue('Email')] = recordRep.getCellValue('Manufacturer');
}

let refsResult = await refs.selectRecordsAsync();
let refsStorage = {},
    ptAux = '',
    refAux = '';

for (let recordRef of refsResult.records) {
    ptAux = recordRef.getCellValue('PT VALUE');
    if (ptAux == null) {
        ptAux = '';
    }
    refAux = recordRef.getCellValue('LIST OR TYPE');
    if (refAux == null) {
        refAux = '';
    }
    refsStorage[recordRef.getCellValue('DOMAIN')] = {
        'point': ptAux,
        'reference': refAux
    };
}

let result = await users.selectRecordsAsync();

let flagUpdate = false,
    objectToUpdate = {},
    domain = '',
    email = '',
    manufacturer = '',
    refValue = '',
    pointValue = '';

for (let record of result.records) {

    if (record.id == newRecordId && record.getCellValue('Email')) {
        domain = record.getCellValue('Domain');
        email = record.getCellValue('Email');
        
        flagUpdate = false;
        objectToUpdate = {};
        
        if (typeof repsStorage[email] != "undefined") {
            manufacturer = repsStorage[email];
            if (typeof manufacturer != "undefined") {
                objectToUpdate['Rep Mfr'] = manufacturer;
                flagUpdate = true;
            }
        }
        
        if (typeof refsStorage[domain] != "undefined" && refsStorage[domain]) {
            refValue = refsStorage[domain]['reference'];
            pointValue = refsStorage[domain]['point'];

            if (typeof refValue != "undefined") {
                objectToUpdate['References'] = refValue;
                flagUpdate = true;
            }

            if (typeof pointValue != "undefined") {
                objectToUpdate['Point Value'] = pointValue;
                flagUpdate = true;
            }
        }

        if (flagUpdate) {
            await users.updateRecordAsync(record, objectToUpdate);
        }
    }
}

Your script is taking a long time because you are updating each record individually and awaiting each update. To speed up the script, store all the updates in an array and do a batch update after your loop (updateRecordsAsync with an s) The batch update method can update 50 records at a time.

3 Likes

Hi @kuovonne, thank you for the anwser.

Your anwser makes sense but actually the function updateRecordAsync is called just one time per execution because it is inside this if condition:

if (record.id == newRecordId && record.getCellValue('Email')) {

In this way I’m updating just the new record. Even if I change for updateRecordsAsync the number of call will be the same. But I’ll try it anyway.

@kuovonne
Even with updateRecordsAsync it happens some times.

Your updateRecordAsync is being called multiple times because it is nested inside the for loop. It is being called once per loop.

for (let record of result.records) {

Do not simply replace the existing updateRecordAsync with updateRecordsAsync. Instead, have your for loop create an array of records to update. Then after the for loop, call updateRecordsAsync.

1 Like

@kuovonne thank you for you help.

I was trying with a break just make a quick test. I refactored it using the right approach.

Until now I was not able to reproduce the problem, so I turned on again and let see if will be enough.

Thank you for now and I’ll keep the thread updated if I get the problem again.

1 Like

I do not know about this process time restriction. Does anyone have a link to this?

There is a long list of restrictions on Scripting actions in this support article.

The processing time is only one. There are also restrictions on total execution time, total mutations, total fetch requests, etc.