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();