recurring incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2018 09:38 AM
I want to have an incident automatically generate for me on a specific day of every month. Is there a way to create a recurring incident that I can close every month and then it will automatically recreate an incident for me at a specific date?
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2018 02:52 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2020 05:21 AM
Hi Teri,
Maybe you already have your solution but I've just wanted to put this here for other people who might are in search for a proper automation.
The request was as following: We need to have maintenance tickets based on a schedule for ICF controls. The recurrence should be possible to be created every second Tuesday of the Month.
My approach was as following:
I wanted it to be dynamic as possible and give the control of the amount of recurrent tickets to the business instead of us creating the templates for them.
What I needed -> Table for Maintenance Tickets -> Table for Maintenance Schedules -> Default Templates table which is already available.
I've created a new table called "Maintenance Schedules" in here I want to keep all my available schedules which I create myself (as the script doesn't cover all options (yet)). You need the following fields in the table.
Name (string): name of the schedule f.e Last Friday of the Month
Interval (choice): Weekly, Monthly
Week Days (list): mon,tue,wed,thu,fri,sat,sun
Recurrence Monthly (choice): Days, On
Month (list): jan,feb,mar,apr.....
Month Days (list): 1,2,3,4,....
Monthly Interval (list): First, Second, Thirdy, Fourth, Last
(note I'm using these abbreviations in my script, you can adjust if needed).
With this I can create my schedules. If I want to have my example "Last Friday of the Month" I need to put in the following values:
Name: Last Friday of the Month
Interval: Monthly
Recurrence Monthly: On
Month: jan,feb,mar......
Monthly Interval: Last
(I'm using client script to show/hide options)
Once your schedule is created and you've added the schedule go to the templates table.
Create a new field on the form called "Maintenance Schedule" reference this to your newly created "Maintenance Schedule" table.
Create the table with your default values you want to have and select the schedule it should run on.
These are the basics and now the script..... I've tried to add as much comment I could think of to make it understandable... Also wrote down things as gs.info so you can see the results via a background script.
You can also run parts of it as background script.
//////////////////////////
//ALL NEEDED DATE VALUES//
//////////////////////////
var gdt = new GlideDateTime();
var currentDate = new GlideDate();
var year = gdt.getYearUTC();
var month = gdt.getMonthUTC();
var month_name = '';
var day = gdt.getDayOfMonthUTC();
var week_day = gdt.getDayOfWeekUTC();
var week_day_name = '';
///////////////////////////////////
//SETTING UP THE DAYS OF THE WEEK//
///////////////////////////////////
if(week_day == '1'){week_day_name = 'mon';}
if(week_day == '2'){week_day_name = 'tue';}
if(week_day == '3'){week_day_name = 'wed';}
if(week_day == '4'){week_day_name = 'thu';}
if(week_day == '5'){week_day_name = 'fri';}
if(week_day == '6'){week_day_name = 'sat';}
if(week_day == '7'){week_day_name = 'sun';}
//////////////////////////
//SETTING UP THE MONTHS //
//////////////////////////
if(month == '1'){month_name = 'jan';}
if(month == '2'){month_name = 'feb';}
if(month == '3'){month_name = 'mar';}
if(month == '4'){month_name = 'apr';}
if(month == '5'){month_name = 'may';}
if(month == '6'){month_name = 'jun';}
if(month == '7'){month_name = 'jul';}
if(month == '8'){month_name = 'aug';}
if(month == '9'){month_name = 'sep';}
if(month == '10'){month_name = 'oct';}
if(month == '11'){month_name = 'nov';}
if(month == '12'){month_name = 'dec';}
///////////////////////////////////////////
//FUNCTION TO CALCULATE xth Day of Month //
///////////////////////////////////////////
function getMonthlyWeekday(n,d,m,y){
var targetDay, curDay=0, i=1, seekDay;
if(d=="mon") seekDay = 1;
if(d=="tue") seekDay = 2;
if(d=="wed") seekDay = 3;
if(d=="thu") seekDay = 4;
if(d=="fri") seekDay = 5;
if(d=="sat") seekDay = 6;
if(d=="sun") seekDay = 7;
while(curDay < n && i < 31){
targetDay = new Date(i++ + " "+m+" "+y);
if(targetDay.getDay()==seekDay) curDay++;
}
if(curDay==n){
targetDay = targetDay.getDate();
return targetDay;
}
else{
return false;
}
}
//This line is to test above function
//gs.info(getMonthlyWeekday(3,'thu','August',2020));
//////////////////////////////////////////////////////////////////
//FUNCTION TO CALCULATE how many "Fridays" there are in a month //
//////////////////////////////////////////////////////////////////
function specificDays(dayName, monthName, year) {
// set names
var monthNames = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
var dayNames = ["sun","mon","tue","wed","thu","fri","sat"];
// change string to index of array
var day = dayNames.indexOf(dayName);
var month = monthNames.indexOf(monthName)+1;
// determine the number of days in month
var daysinMonth = new Date(year, month, 0).getDate();
// set counter
var sumDays=0;
// iterate over the days and compare to day
for(var i=1; i<=daysinMonth; i++) {
var checkDay = new Date(year, month-1, parseInt(i)).getDay();
if(day == checkDay) {
sumDays ++;
}
}
// show amount of day names in month
return sumDays;
}
////////////////////////////
//NOW THE FUN STUFF BEGINS//
////////////////////////////
//Query on all the templates, which are using the maintenance table and are active
var templates = new GlideRecord('sys_template');
templates.addQuery('table','u_maintenance');
templates.addQuery('active',true);
templates.query();
while(templates._next()){
//Retrieving the schedule attached to the template
var schedule = new GlideRecord('u_maintenance_schedules');
schedule.addQuery('sys_id',templates.u_maintenance_schedule);
schedule.query();
//Checking if the schedule can run yes or no
while(schedule.next()){
/////////////////////////////////
// THIS IS THE WEEKLY INTERVAL //
/////////////////////////////////
if(schedule.u_interval == 'weekly') {
gs.info("Okay Im indeed scheduled on a weekly base");
//Getting the days and put them in an array to loop through
var scheduled_days = schedule.u_week_days;
var splitDays = scheduled_days.split(",");
//the answer we got from the loop
var runToday = '';
gs.info(week_day_name);
//Looping through the days if we can run yes or no
for(i = 0; i < splitDays.length; i++) {
gs.info("Current day to check " + splitDays[i]);
if(week_day_name == splitDays[i]) {
runToday = 'true';
}
}
if(runToday == 'true'){
gs.info("Yes I've found a valid template");
gs.info("My template name is " + templates.name);
var mntWeekly = new GlideRecord("u_maintenance");
mntWeekly.initialize();
mntWeekly.applyTemplate(templates.name);
mntWeekly.insert();
}
//RunToday should always be set to false after every major interval script.
runToday = 'false';
}
/////////////////////////////////////////
// THIS IS THE MONTHLY - DAYS INTERVAL //
/////////////////////////////////////////
if(schedule.u_interval == 'monthly' && schedule.u_recurrence_monthly == 'days') {
gs.info("I'm a monthly schedule");
//Getting the to run months and putting them in an array to loop through
var scheduled_months = schedule.u_month;
var splitMonths = scheduled_months.split(",");
gs.info("Current month is " + month + " - " + month_name);
for(m = 0; m < splitMonths.length; m++) {
gs.info("Current month to check " + splitMonths[m]);
if(month_name == splitMonths[m]){
gs.info("CurrentMonth " + month_name + " is matching the schedule month " + splitMonths[m]);
//So the month is fine to run, but now we need to check the day.
var daysOfMonth = schedule.u_days;
var splitDays = daysOfMonth.split(",");
var runToday = '';
for(d = 0; d < splitDays.length; d++) {
gs.info("Current day to check " + splitDays[d]);
if(day == splitDays[d]){
runToday = 'true';
}
}
}
if(runToday == 'true'){
gs.info("Yes I have a template matched to run Today");
gs.info("My template name is " + templates.name);
var mntMonthly = new GlideRecord("u_maintenance");
mntMonthly.initialize();
mntMonthly.applyTemplate(templates.name);
mntMonthly.insert();
}
//RunToday should always be set to false after every major interval script.
runToday = 'false';
}
}
///////////////////////////////////////
// THIS IS THE MONTHLY - ON INTERVAL //
///////////////////////////////////////
if(schedule.u_interval == 'monthly' && schedule.u_recurrence_monthly == 'on'){
gs.info("I'm a monthly - on schedule");
//First defining the "Monthly Interval" first, second, third, fourth, last -- mon, tue... of the month
var weekDay = schedule.u_week_days;
var weekDaySplit = weekDay.split(",");
var dayofweek = '';
for(dow = 0; dow < weekDaySplit.length; dow++){
gs.info("Check schedule day " + weekDaySplit[dow] + " against current day " + week_day_name);
//If we have a match on the weekday then we continue
if(week_day_name == weekDaySplit[dow]){
var intervalMonth = schedule.u_monthly_interval;
var intervalMonthSplit = intervalMonth.split(",");
//We are already setting the interval.... if the interval is mentioned like 1st, 2nd..
//This is needed later on in the script.
for(imon = 0; imon < intervalMonthSplit.length; imon++){
gs.info("We are checking for the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of the month ");
var month = schedule.u_month;
var monthSplit = month.split(",");
//Now we are going the check the month which is given so we now which interval to check. f.e. 2nd Monday of August
for(mon = 0; mon < monthSplit.length; mon++){
gs.info("Check the month " + monthSplit[mon] + " against the current month " + month_name);
if(month_name == monthSplit[mon]){
//The month given in the interval matches the current month so we can continue.
//Checking which interval to use.
if(intervalMonthSplit[imon] == 'first'){
//Putting all the necessary variables and calling the function getMonthlyWeekday (which on top of this whole script)
//The information is passed through like getMonthlyWeekday(1,'thu','aug',2020') - this will return '6'
var targetDay = getMonthlyWeekday(1,week_day_name,monthSplit[mon],year)
//Receiving the target day... and check if it matches the day of Today.
if(targetDay == day){
gs.info("I can run, because it's the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of " + monthSplit[mon]);
var runToday = true;
}
}
if(intervalMonthSplit[imon] == 'second'){
//Putting all the necessary variables and calling the function getMonthlyWeekday (which on top of this whole script)
//The information is passed through like getMonthlyWeekday(2,'thu','aug',2020') - this will return '13'
var targetDay = getMonthlyWeekday(2,week_day_name,monthSplit[mon],year)
//Receiving the target day... and check if it matches the day of Today.
if(targetDay == day){
gs.info("I can run, because it's the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of " + monthSplit[mon]);
var runToday = true;
}
}
if(intervalMonthSplit[imon] == 'third'){
//Putting all the necessary variables and calling the function getMonthlyWeekday (which on top of this whole script)
//The information is passed through like getMonthlyWeekday(1,'thu','aug',2020') - this will return '20'
var targetDay = getMonthlyWeekday(3,week_day_name,monthSplit[mon],year);
gs.info("Is target day defined? " + targetDay);
//Receiving the target day... and check if it matches the day of Today.
if(targetDay == day){
gs.info("I can run, because it's the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of " + monthSplit[mon]);
var runToday = true;
}
}
if(intervalMonthSplit[imon] == 'fourth'){
//Putting all the necessary variables and calling the function getMonthlyWeekday (which on top of this whole script)
//The information is passed through like getMonthlyWeekday(1,'thu','aug',2020') - this will return '27'
var targetDay = getMonthlyWeekday(4,week_day_name,monthSplit[mon],year);
//Receiving the target day... and check if it matches the day of Today.
if(targetDay == day){
gs.info("I can run, because it's the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of " + monthSplit[mon]);
var runToday = true;
}
}
if(intervalMonthSplit[imon] == 'last'){
//Putting all the necessary variables and calling the function specificDays (which is on top of this whole script)
//The information is passed through like specificDays('thu','aug',2020') - this will return '4'
var nthMonth = specificDays(week_day_name,monthSplit[mon],year);
//Putting all the necessary variables and calling the function getMonthlyWeekday (which on top of this whole script)
//The information is passed through like getMonthlyWeekday(4,'thu','aug',2020') - this will return '27'
var targetDay = getMonthlyWeekday(nthMonth,week_day_name,monthSplit[mon],year);
//Receiving the target day... and check if it matches the day of Today.
if(targetDay == day){
gs.info("I can run, because it's the " + intervalMonthSplit[imon] + " " + weekDaySplit[dow] + " of " + monthSplit[mon]);
var runToday = true;
}
}
}
}
}
}
}
if(runToday == true){
gs.info("Yes I have a template matched to run Today");
gs.info("My template name is " + templates.name);
var mntMonthlyOn = new GlideRecord("u_maintenance");
mntMonthlyOn.initialize();
mntMonthlyOn.applyTemplate(templates.name);
mntMonthlyOn.insert();
}
//RunToday should always be set to false after every major interval script.
runToday = 'false';
}
////////////////////////////////////////////
// ADDITIONAL INTERVAL CAN BE ADDED BELOW //
////////////////////////////////////////////
}
}
Kr!
Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2020 05:33 AM
😮