Help

Re: Scripting Block Issue

3413 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Josh_Cyk
5 - Automation Enthusiast
5 - Automation Enthusiast

I’m currently running a script that creates a set of records. There are 67 records that need to get created. Usually it works. However, sometimes only a portion of the records are created. If I run the script a second time it works. Why does this happen and how do I resolve this?

10 Replies 10

Hi @Josh_Cyk - there’s no obvious reason from your description why this would happen and I haven’t seen anything like this with my own scripts. My guess is there’s something specific in the script - are you able to post it here?

Are you using createRecordsAsync? It has a batch limit of 50 records. In order to create more than 50 records, you need to split the list of records up into batches of 50 or less.

        while (records.length > 0) {
            await table.createRecordsAsync(records.slice(0, 50));
            records = records.slice(50);
        }

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

let projects = base.getTable('Jobs');
let tasks = base.getTable('Estimates');
let jobRoles = base.getTable('Levi Job Roles/Categories'), jobRolesRecords = await jobRoles.selectRecordsAsync();
let selectedRecord = await input.recordAsync('Select a record to use', projects);
let yearlyRates = base.getTable('Yearly Rates') , yearlyRatesRecords = await yearlyRates.selectRecordsAsync();

let category = selectedRecord.getCellValueAsString("Marketing/PDP?")
output.text(category);
let jobId = selectedRecord.id
let yearValue = selectedRecord.getCellValueAsString("Job Start Year")
let yearRecord;
for (let record of yearlyRatesRecords.records) {
  if (record.getCellValueAsString("Year") == yearValue){
yearRecord = record
output.text(yearValue);     
break;
  }  
}
for (let record of jobRolesRecords.records) {
//break;
if (category != "Marketing") {
    let isPdp = record.getCellValueAsString("PDP")
    if (isPdp == "checked") {
        await tasks.createRecordAsync(
            {
                "Job": [{ id: jobId }],
                "Levi's Job Role": [{ id: record.id }],
                "Yearly Rates" : [{ id: yearRecord.id }]
            }
        );
    }
} else {
    let isMarketing = record.getCellValueAsString("Marketing")
    if (isMarketing == "checked") {
        await tasks.createRecordAsync(
            {
                "Job": [{ id: jobId }],
                "Levi's Job Role": [{ id: record.id }],
                "Yearly Rates" : [{ id: yearRecord.id }]
            }
        );
    }

}
// break;
}

output.text('Done!');

Hi @Josh_Cyk - nothing obvious in the code that I can see that would cause the specific issue you are talking about. So either, 1) I’m missing something obvious or 2) the interaction of script with the data is causing issues or 3) something else entirely!

Some other comments:

  • You’re picking a project/record at the start. From your first post you want to create 67 records, but perhaps there’s something in the data of different projects that is stopping this.

  • Your if/else statement is confusing me. It looks like you’re saying:

“if these conditions are true, update tasks with these values; otherwise update tasks with the same values”

Obviously, I don’t know what how your base is structured and what data you have in it, but I would generally expect something more like:

If these conditions are true, do A; otherwise do B.

  • Your category appears to be a binary choice:

let category = selectedRecord.getCellValueAsString("Marketing/PDP?")

but you’ve also got isMarketing and is PDP checkboxes. You could probably simplify this (if my assumption is correct) by just checking the category value in the if/else statement.

Hope this helps. As a above, I’m making a lot of assumptions as I go as I can’t see the base or the data, so I may be completely wrong.

This clue may be related to a few issues that are banging against each other - my @0.02

  1. The first is that all APIs (including the Script Block apparently) are subject to quotas. It could be simply a quota breach that is intermittent. Just because you are using await to create records, it doesn’t mean it’s not doing too fast for the limits imposed.

  2. Another is the logic (as @JonathanBowen intimated) - perhaps there’s a condition that you’re not entirely familiar with as the process runs. Sometimes running a second time will change the conditions just enough to mimick the conditions that you expected in the initial run. I call these Heisenbugs. :winking_face:

Is there any simple way to increase the batch limit?

Welcome to the Airtable community!

No, you cannot increase the limits set by Airtable. If you might have more records than will fit in a single batch, you must break it into multiple batches.

Thank you!

So practically speaking if and I have a large DB, then break this into multiple batches and consolidate afterwards correct in AT?

…and just out of curiosity why are the limits set in place?

If you have a lot of changes, you need multiple batches. It is possible to have a large database but make only a handful of changes (create, update, delete).

Having limits is normal and sensible. It takes the server time to process changes, and having to process tens of thousands of records in a single batch would slow things down too much. Having batch limits makes sure that Airtable’s servers aren’t suddenly slammed with massive changes and gives everyone a chance to get their requests in.