Help

Re: Why is my createRecordAsync loop terminating at 15 records?

1125 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Binthout_BV
5 - Automation Enthusiast
5 - Automation Enthusiast

The strangest things happens when I execute the simple below script in Airtable.
Does anyone know why it just stops after 15 created records and does not finish up to 50?

let table = base.getTable(“Clients”);

for (let i=0; i<50; i++) {
table.createRecordAsync({“Number”: i});
}

8 Replies 8

Yes - you’re [probably] overrunning the API quota limit of 5 calls per second.

Oh, wait - this is the scripting block. Not sure.

Scripting block has an unpublished rate limit. I ran into that rate limit a few times before I decided that I didn’t want to mess with it and just made things run in series.

To make it run in series, use await in front of your function call.

You are also creating only one record at a time, not a batch of 50. If you want to create records in batches of 50, use createRecordsAsync (note the s) and use the loop in the example script with slice.

// Only up to 50 updates are allowed at one time, so do it in batches
        while (updates.length > 0) {
            await table.updateRecordsAsync(updates.slice(0, 50));
            updates = updates.slice(50);
        }

You will also need to format your records as an array.

async function (records: Array‹{[fieldNameOrId: string]: unknown}›)

The documentation on the web is much easier to read than the documentation in Scripting block itself.


If this answers your question, please mark this post as the solution. Otherwise, could you please give a bit more details and a screen capture?

you need to use await infront of your call to table.createRecordAsync(). You’re having a race condition.

Binthout_BV
5 - Automation Enthusiast
5 - Automation Enthusiast

Thanks guys for your responses.

I read somewhere that the writing limit is 15 records/sec, so that’s confirmed ;-).

If I add ‘await’, it takes 1 sec for every record to create, so I like your batching methode. But I didn’t mention yet that I also want to add content to the new records, not just create it.
So I added a timeout in between every batch of 15; and it works :slightly_smiling_face: (even though airtable doesn’t seem to recognize ‘setTimeout’ - does anyone know why?):

let table = base.getTable("Clients");
let batchsize = 15
let batches = 3
let i

function createRecords(j) {
    for (i=j; i<j+batchsize; i++) {
        table.createRecordAsync({"Number": i});
    }
}

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

for (let batch=1; batch<=batches; batch++) {
    createRecords(1+(batch-1)*batchsize);
    await timeout(1500); //1.5s pause
}
Ashwin_P
6 - Interface Innovator
6 - Interface Innovator

Hey,

The “ createRecordsAsync” API is designed for this purpose. It can update 50 records in one call.

Glad you got your script working!

If you remember where you saw that rate limit, could you please post it?


You can put your content in an array, then submit a single createRecordsAsync request with the array of content. If you have more than 50 records (batch limit) you will have to split of your batches.

let recordsData = [];
for (let i = 0; i < 15; i++) {
  recordsData.push({fields: {"Number": i}});
}
table.createRecordsAsync(recordsData);

I don’t know why. There was a discussion about setTimeout on this thread. There have also been other times when Scripting block flags things that work just fine. Hopefully as Scripting block matures these wrinkles will get worked out.

(I was originally posting as Binthout_BV, but that was my boss’s account)

That’s great kuovonne, really helpful for future app’s!
I don’t remember where I read about the 15 record write-limit, but if I try 16 records it just stops after 15.

Hi, this is likely old news by now, but must be the reference that @Binthout_BV cited and you were asking for:
How to Work Around API Limits