Help

Build Junction Table Script Exceeded Execution Time Limit

Topic Labels: Automations
Solved
Jump to Solution
736 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Dominic
6 - Interface Innovator
6 - Interface Innovator

Hi,

I have written a script that is triggered within an automation that essentially builds out a junction table. The issue is that it frequently fails with the error "Script exceeded execution time limit of 30 seconds". Does anyone know of ways I could edit this so that it will more likely run within the 30sec time limit?

Many thanks in advance!

 

let classesTable = base.getTable('Classes');
let clientsTable = base.getTable('Clients');
let attendanceTable = base.getTable('Attendance');
let attendanceQuery = await attendanceTable.selectRecordsAsync();

// Get the record ID from the automation input
let triggeringRecordId = input.config().recordId;

// Retrieve all the records in the Classes table
let classesQuery = await classesTable.selectRecordsAsync();
let classesRecords = classesQuery.records;

// Find the triggering record using the provided ID
let classesRecord = classesRecords.find(record => record.id === triggeringRecordId);
let classDate = classesRecord.getCellValue('Class');
let scenesToConduct = classesRecord.getCellValue('Scenes to Conduct');
let clientsInRoster = classesRecord.getCellValue('Clients in Roster');

// If any required field is empty, return and quit the script
if (!classDate || !scenesToConduct || !clientsInRoster) {
return;
}

// Loop through all scenes and clients to generate unique combinations
for (let scene of scenesToConduct) {
for (let clientId of clientsInRoster) {

// Check if the combination already exists in the Attendance table
let existingRecord = attendanceQuery.records.find(record =>
record.getCellValue('Class')[0].id === classesRecord.id &&
record.getCellValue('Scene').id === scene.id &&
record.getCellValue('Client').id === clientId
);

// If the combination doesn't exist, create a new record in the Attendance table
if (!existingRecord) {
await attendanceTable.createRecordAsync({
'Class': [{id: classesRecord.id}],
'Scene': [{id: scene.id}],
'Client': [{id: clientId}],
});
}
}
}

 

1 Solution

Accepted Solutions
Dominic
6 - Interface Innovator
6 - Interface Innovator

Thanks for the suggestion Adam. Yes, I do realise that is best practice and a good point. In this cace the script is never writing more than about 20 records anyway, so here it would not improve the issue.

Having said that, I did some more digging and realised that the main reason the script was taking so long to run was because the table it is writing to, and the one it was checking for existing records to prevent duplication, already had nearly 30k historical records. By changing the script to use only check a filtered view of recent records, I managed to ensure that records were not added by accidental double clicking of the button that triggers the script, while reducing the run time to under 5 secs. 

See Solution in Thread

2 Replies 2
TheTimeSavingCo
18 - Pluto
18 - Pluto

It looks like you're creating single records.  Could you try creating them in batches instead via createRecordsAsync?

So something like:

 

let updates = new Array

if (!existingRecord) {
    updates.push({
        'Class': [{id: classesRecord.id}],
        'Scene': [{id: scene.id}],
        'Client': [{id: clientId}],
    })  
}

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

 

 

Dominic
6 - Interface Innovator
6 - Interface Innovator

Thanks for the suggestion Adam. Yes, I do realise that is best practice and a good point. In this cace the script is never writing more than about 20 records anyway, so here it would not improve the issue.

Having said that, I did some more digging and realised that the main reason the script was taking so long to run was because the table it is writing to, and the one it was checking for existing records to prevent duplication, already had nearly 30k historical records. By changing the script to use only check a filtered view of recent records, I managed to ensure that records were not added by accidental double clicking of the button that triggers the script, while reducing the run time to under 5 secs.