diff --git a/Server-Side Components/Scheduled Jobs/Recurring Task Automation/readme.md b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/readme.md new file mode 100644 index 0000000000..caa82bd6e9 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/readme.md @@ -0,0 +1,3 @@ +The Recurring Task Automation project automates the creation of recurring tasks in ServiceNow based on a predefined schedule or trigger. This ensures routine operational tasks, maintenance jobs, or checks are executed consistently without manual intervention. + +Unlike OOB scheduled jobs for individual tasks, this solution is dynamic, flexible, and configurable per task type. diff --git a/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scheduled job.js b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scheduled job.js new file mode 100644 index 0000000000..5846a5c07a --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scheduled job.js @@ -0,0 +1,2 @@ +var generator = new RecurringTaskGenerator(); +generator.generateTasks(); diff --git a/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scriptInclude.js b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scriptInclude.js new file mode 100644 index 0000000000..9febb519f8 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Recurring Task Automation/scriptInclude.js @@ -0,0 +1,39 @@ +var RecurringTaskGenerator = Class.create(); +RecurringTaskGenerator.prototype = { + initialize: function() {}, + + generateTasks: function() { + var templateGR = new GlideRecord('u_recurring_task_template'); + templateGR.addActiveQuery(); + templateGR.query(); + + while (templateGR.next()) { + var lastRun = templateGR.last_run_date || templateGR.start_date; + var nextRun = new GlideDateTime(lastRun); + + // Calculate next run based on frequency + if (templateGR.frequency == 'daily') nextRun.addDaysUTC(1); + else if (templateGR.frequency == 'weekly') nextRun.addDaysUTC(7); + else if (templateGR.frequency == 'monthly') nextRun.addMonthsUTC(1); + + var now = new GlideDateTime(); + if (nextRun <= now) { + // Create new task from template + var newTask = new GlideRecord('task'); + newTask.initialize(); + newTask.short_description = templateGR.template_task.short_description + ' (Recurring)'; + newTask.description = templateGR.template_task.description; + newTask.assignment_group = templateGR.assignment_group; + newTask.assigned_to = templateGR.assigned_to; + newTask.parent = templateGR.template_task; // optional link + newTask.insert(); + + // Update last run date + templateGR.last_run_date = now; + templateGR.update(); + } + } + }, + + type: 'RecurringTaskGenerator' +};