Scripting Block Issue

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?

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.

1 Like

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. :wink: