How do you schedule recurring changes automatically within Change Mgmt App?

Katie Fent
Mega Contributor

What have you all done to schedule recurring changes within Change app? 

 

 

9 REPLIES 9

I agree. You could use similar logic to what I explained with our days of the week fields and create a "firstThursdayOfTheMonth" function based on which day of the week the first falls on.  so you know if the 1st is on Wednesday, the first Thursday is the 2nd, etc...

If you're interested, here's the code from my scheduled job that I described above

//Determine which day of the week the first of the month will be for the upcoming month
var date = gs.beginningOfNextMonth(gdt);
var gdt = new GlideDateTime(date);
var firstOfMonth=gdt.getDayOfWeekLocalTime();
var nextPatchYear= gdt.getYearLocalTime();
var nextPatchMonth=gdt.getMonthLocalTime();
var patchDate;


switch(firstOfMonth) {
  case 1:
    dayString='Monday';
    break;
  case 2:
    dayString='Tuesday';
    break;
  case 3:
    dayString='Wednesday';
    break;
  case 4:
    dayString='Thursday';
    break;
  case 5:
    dayString='Friday';
    break;
  case 6:
    dayString='Saturday';
    break;
  case 7:
    dayString='Sunday';
    break;
}

//create patch dat variable based on the first of the month.
//gs.info('First Day of Next Month is: ' +dayString);
patchDateVar='u_first_is_' +dayString.toLowerCase();


//Go through all the schedules and create a corresponding change_request for next month's patching. The date of the change is determined by the first of the month.

var schedule = new GlideRecord("x_llbe_server_patc_server_patch_schedule"); 
schedule.query(); 
while (schedule.next()) { 
	
	//create a new change Request
	var changeRequest = new GlideRecord("change_request"); 
	changeRequest.initialize();
	
	//apply the template defined for this schedule
	var t = new GlideTemplate.get(schedule.u_change_template);   
	t.apply(changeRequest); 
	
	//add all servers as affected CIs
	var serverList = schedule.u_server_list.split(',');
	var serverListString='';
	
	for (i=0; i<serverList.length; i++) {
		// Create a new 'Affected CI' record for each
		var aci = new GlideRecord('task_ci');
		aci.initialize();
		aci.task = changeRequest.sys_id; // Associate to this record
		aci.ci_item = serverList[i];
		serverListString = serverListString +aci.ci_item.getDisplayValue() +',' ;

		aci.insert();
	}
	
	serverListString.substring(0, serverListString.length - 1);
	changeRequest.short_description= 'Patch ' +schedule.u_name + ':' +serverListString.substring(0, serverListString.length - 1);
	changeRequest.description= 'Unix Patch: ' +serverListString.slice(0,-1) +'\nADDITIONAL DETAILS IF SUPPLIED:\n' +schedule.u_details;
	

	//get date level info for next patch date	
	patchDate=nextPatchYear +"-" +nextPatchMonth +"-" +schedule.getValue(patchDateVar);
	
	//get Start Time info for next 
	var startTimeGDT = new GlideDateTime(schedule.u_start_time);
	var startDateTimeArray = startTimeGDT.getDisplayValue().split(' ');
    var startTime = startDateTimeArray[1];
	
	changeRequest.start_date= patchDate +' ' +startTime;
			
	
	//calculate end time by starting with tart_date and adding duration
	var duration = schedule.u_patch_duration.dateNumericValue();
	var tempTime = new GlideDateTime(changeRequest.start_date);
	tempTime.add(duration);
	gs.info('SUCCESSFULLY ADDED DURATION');
	changeRequest.end_date=tempTime;
	gs.info('CHANGE END TIME IS: ' +changeRequest.end_date.getDisplayValue());
	
	changeRequest.u_environment=schedule.u_environment;
		
	//changeRequest.end_date=changeRequest.end_date;
	
	
changeRequest.insert();

} 

I would like to use the variables you set but have them using 1-4 for the x day of the month ie the 2nd Monday of the month i would put 2 in the Monday field.

How would you go about getting those dates?

Rachael7
Tera Contributor

I have requested that ServiceNow add this as out-of-box functionality within their Idea Portal. If you like, you can vote it up so that it is looked at sooner. 

williame
Tera Contributor

I modified @Brian Bouchard 's script to work with day of week month. I also used part of a script found on stackoverflow.

 

I am sure there are probably some loops or something that could make this script better - I am learning 🙂 

We are only allowing the fields on the form to be 1-4 , to correspond with the week.

 

//Determine which day of the week the first of the month will be for the upcoming month
var date = gs.beginningOfNextMonth(gdt);
var gdt = new GlideDateTime(date);

function nthDayOfMonth(day, n, date) {
    var count = 0,
        idate = new Date(date);
    idate.setDate(1);

    while (count < n) {
        idate.setDate(idate.getDate() + 1);
        if (idate.getDay() == day) {
            count++;
        }
    }

    return idate;
}


//Go through all the schedules and create a corresponding change_request for next month's patching. The date of the change is determined by the first of the month.
var schedule = new GlideRecord("u_scheduled_patching_changes");
schedule.query();
while (schedule.next()) {
    var sunday = schedule.u_first_is_sunday;
	var monday = schedule.u_first_is_monday;
	var tuesday = schedule.u_first_is_tuesday;
	var wednesday = schedule.u_first_is_wednesday;
	var thursday = schedule.u_first_is_thursday;
	var friday = schedule.u_first_is_friday;
	var saturday = schedule.u_first_is_saturday;
	
	if (sunday > 0 && sunday <= 4) {
		vardayofweeksunday = '0';
        var today = new Date();
        // ( DayofWeek (Sunday) , Week(1st/2nd/3rd/4th) , today )
        var res = nthDayOfMonth(vardayofweeksunday, sunday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweeksunday, sunday, plusOne);

	} else if (monday > 0 && monday <= 4) {
		vardayofweekmonday = '1';
        //Today
        var today = new Date();
        // ( DayofWeek (monday) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweekmonday, monday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweekmonday, monday, plusOne);

	}
	else if (tuesday > 0 && tuesday <= 4) {
		vardayofweektuesday = '2';
        //Today
        var today = new Date();
        // ( DayofWeek (tuesday ) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweektuesday, tuesday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweektuesday, tuesday, plusOne);

	}
	else if (wednesday > 0 && wednesday <= 4) {
		vardayofweekwednesday = '3';
        //Today
        var today = new Date();
        // ( DayofWeek (wednesday ) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweekwednesday, wednesday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweekwednesday, wednesday, plusOne);

	}
	else if (thursday > 0 && thursday <= 4) {
		vardayofweekthursday = '4';
        //Today
        var today = new Date();
        // ( DayofWeek (thursday ) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweekthursday, wednesday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweekthursday, wednesday, plusOne);

	}else if (friday> 0 && friday <= 4) {
		vardayofweekfriday = '5';
        //Today
        var today = new Date();
        // ( DayofWeek (friday ) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweekfriday, wednesday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweekfriday, wednesday, plusOne);

	}
	else if (saturday > 0 && saturday <= 4) {
		vardayofweeksaturday = '6';
        //Today
        var today = new Date();
        // ( DayofWeek (saturday ) , Week(1st/2nd/3rd/4th) )
        var res = nthDayOfMonth(vardayofweeksaturday, wednesday, today);
        var plusOne = new Date(res);
        plusOne.setMonth(plusOne.getMonth() + 1);
        var res2 = nthDayOfMonth(vardayofweeksaturday, wednesday, plusOne);

	}


    //create a new change Request
    var changeRequest = new GlideRecord("change_request");
    changeRequest.initialize();

    //apply the template defined for this schedule
    var t = new GlideTemplate.get(schedule.u_template_change);
    t.apply(changeRequest);

    //add all servers as affected CIs
    var serverList = schedule.u_server_list.split(',');
    var serverListString = '';

    for (i = 0; i < serverList.length; i++) {
        // Create a new 'Affected CI' record for each
        var aci = new GlideRecord('task_ci');
        aci.initialize();
        aci.task = changeRequest.sys_id; // Associate to this record
        aci.ci_item = serverList[i];
        serverListString = serverListString + aci.ci_item.getDisplayValue() + ',';

        aci.insert();
    }
    serverListString.substring(0, serverListString.length - 1);
    changeRequest.short_description = 'Patch ' + schedule.u_schedule_name + ':' + serverListString.substring(0, serverListString.length - 1);
    changeRequest.description = 'Patch: ' + serverListString.slice(0, -1) + '\nADDITIONAL DETAILS IF SUPPLIED:\n' + schedule.u_details;
	changeRequest.work_notes = "Ticket auto created via the Auto Patching script using Servicenow Template: " + schedule.u_template_change.getDisplayValue() + "\n Scheduled Patching Changes form: \n" + schedule.u_schedule_name + "\n Record: \n" + schedule.u_number;
    
	//get date level info for next patch date	
    // setting the date!
    var date2 = res2;
    var gdt2 = new GlideDateTime();
    gdt2.setDisplayValue(date2, "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'");

    //get Start Time info for next 
    var startTimeGDT = new GlideDateTime(schedule.u_start_time_2);
    var startDateTimeArray = startTimeGDT.getDisplayValue().split(' ');
    var startTime = startDateTimeArray[1];
    changeRequest.start_date = gdt2.getDate() + ' ' + startTime;

    //calculate end time by starting with tart_date and adding duration
    var duration = schedule.u_patch_duration.dateNumericValue();
    var tempTime = new GlideDateTime(changeRequest.start_date);
    tempTime.add(duration);

	// Subtracting 5hours (timezone issue) was adding 5hours to end time.
	tempTime.addSeconds(-18000);
    changeRequest.end_date = tempTime;
    changeRequest.u_environment = schedule.u_enviornment;
	//gs.log('BILLE CHNG ENV: ' + changeRequest.u_environment + "Schedule ENV " + schedule.u_enviornment);
    changeRequest.insert();
}

 

bpscotty
Tera Contributor

Could you share the table xml / update set please