Help

Re: Scripting Error

145 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Dpond
4 - Data Explorer
4 - Data Explorer

Hi, I am very new to scripting and managing a base for content production.  Want to create a script that will update a series of dates based off the publication date changing.  I'm getting errors and not sure why.  Any help would be appreciated.  Thanks

Here is the script

let table = base.getTable("Deliverables");

async function updateRecord() {
let inputConfig = input.config();
let recordId = inputConfig.recordId; // Fetch the record ID from the previous step

// Check if recordId is valid (a string and not undefined)
if (typeof recordId !== 'string' || !recordId.trim()) {
throw new Error("Invalid or undefined record ID. Ensure the input provides a valid record ID.");
}

// Fetch the specific record from the table
let record = await table.selectRecordAsync(recordId);

if (!record) {
throw new Error(`Record with ID ${recordId} not found.`);
}

let publishDate = record.getCellValue("Published Date");
let Series = record.getCellValue("Series");

if (publishDate) {
let updates = {};

if (Series === "Catalyst") {
updates["V4 Due (Finishing)"] = subtractBusinessDays(publishDate, 1);
updates["V1 Due (Rough)"] = subtractBusinessDays(publishDate, 8);
updates["V2 Due (Rough)"] = subtractBusinessDays(publishDate, 4);
updates["V3 Due (Fine)"] = subtractBusinessDays(publishDate, 2);
updates["Paper Cut Due"] = subtractBusinessDays(publishDate, 9);
updates["GFX Start Date"] = subtractBusinessDays(publishDate, 4);
}

if (Series === "This is Working") {
updates["V1 Due (Rough)"] = subtractBusinessDays(publishDate, 9);
updates["V2 Due (Rough)"] = subtractBusinessDays(publishDate, 7);
updates["V3 Due (Fine)"] = subtractBusinessDays(publishDate, 4);
updates["V4 Due (Finishing)"] = subtractBusinessDays(publishDate, 1);
updates["Paper Cut Due"] = subtractBusinessDays(publishDate, 9);
updates["GFX Start Date"] = subtractBusinessDays(publishDate, 7);
}

if (Series === "The Path") {
updates["Paper Cut Due"] = subtractBusinessDays(publishDate, 19);
updates["V1 Due (Rough)"] = subtractBusinessDays(publishDate, 16);
updates["V2 Due (Rough)"] = subtractBusinessDays(publishDate, 14);
updates["V3 Due (Fine)"] = subtractBusinessDays(publishDate, 11);
updates["GFX Start Date"] = subtractBusinessDays(publishDate, 14);
updates["V4 Due (Finishing)"] = subtractBusinessDays(publishDate, 7);
}

if (Object.keys(updates).length > 0) {
await table.updateRecordAsync(record.id, updates);
}
}
}

// Function to subtract business days
function subtractBusinessDays(date, days) {
let result = new Date(date);
let count = 0;
while (count < days) {
result.setDate(result.getDate() - 1);
if (result.getDay() !== 0 && result.getDay() !== 6) { // Skip weekends
count++;
}
}
return result;
}

// Call the function
updateRecord();

4 Replies 4

What errors are you getting?  Could you provide access to a duplicate of your base with some example data and the script set up in it?  It would make it much easier to help you!

Alexey_Gusev
13 - Mars
13 - Mars

Hi,

There are 2 types of scripts in Airtable (actually four, but here other two are not so important): Automation script and Scripting Extension. Your script mixes both methods that's why it won't work until you decide which one to run. Automation usually triggered by event in some record, then you can use record data in further steps including script. Extension usually launched by button and you can use it to process the whole table or view if you want.
Script here is almost ready to go with automation. All you need is to select a trigger (for example, add checkbox field and choose 'when record matches condition' (' CheckboxField' is ✔). Then you can install your script in script step.

Alexey_Gusev_0-1738633049516.png

 


You need to choose input data in editor left side

Alexey_Gusev_1-1738633205281.png

Actually. if you lucky enough, you can try to run it after that.

The only thing to fix - change last line to  

// Call the function
await updateRecord();


btw, I would recommend to use this tag for better readability of your code

Alexey_Gusev_2-1738633409855.png



It is not necessary to do now, but it's a good example for future cases -  if you add all input variables instead of querying them from table, script part will be much easier. That's a point of automation scripting - you can set input / output data by GUI instead of scripting them. That's how it can be much shorter in a beginning third part of the script (I didn't paste whole code, because I have no such fields)

Alexey_Gusev_3-1738634035560.png

 

 

Dpond
4 - Data Explorer
4 - Data Explorer

@Alexey_Gusev  I made the updates above, and the script is working in that the testing is no longer throwing errors, but the issue is, the record isnt being updated with the new dates.

Screenshot 2025-02-04 at 8.31.38 AM.pngScreenshot 2025-02-04 at 8.32.10 AM.pngScreenshot 2025-02-04 at 8.32.40 AM.pngScreenshot 2025-02-04 at 8.32.52 AM.png

Screenshot 2025-02-04 at 8.27.13 AM.png

 

 

Hi,
did you change the end line of script, adding await ?

// Call the function
await updateRecord();

If yes, and still no updates, add debug line here:

if (Object.keys(updates).length > 0) {
console.log(updates)  // debug line
await table.updateRecordAsync(record.id, updates);

by the way,  the line await table.updateRecordAsync(record.id, updates); should display some warnings because you have no variable 'record'.
I think, you should put recordId instead of record.id