Need to auto fill Planned Start date of Change request which automatically creating from Schedule

Arjun Reddy Yer
Mega Sage

required help @Ankur Bawiskar @SumanthDosapati @Vasantharajan N @Mohammed8 

 

With this script Change Request is getting created and Change Task also getting created and auto assigned to group.

As I need to auto fill the Planned Start date field with the 2nd Thursday of the month.

As the Schedule Job is running on every month 1st day.

Schedule Job:

    var templateName = 'Monthly PMPC Patching'; // Replace with your template name
    var change = new GlideRecord('change_request');
    change.initialize();
    change.std_change_producer_version = gs.getProperty('SJRM02022026_std_change_producer_version_pmpc'); // Standard Change Templates version
    change.applyTemplate('Monthly PMPC Patching');

    // Insert the change request

    change.chg_model = gs.getProperty('SJRM02022026_chg_model'); // Change model is standard
    change.assignment_group = gs.getProperty('SJRM02022026_chg_assignment_group'); //Assignment Group: Change Management
    var changeSysId = change.insert();
    gs.info('Standard change request created: ' + changeSysId);

    // Change Task Creation

    var rec = new GlideRecord('change_task');
    rec.initialize();
    rec.change_request = changeSysId;
    rec.short_description = 'Merchandising Quality Engineering Testing';
    rec.description = 'Confirm business application functions as it should with new Microsoft patches installed.';
    rec.assignment_group.setDisplayValue('Merchandising Quality Engineering');
    rec.insert();
1 ACCEPTED SOLUTION

It's working with below script thanks for sharing the script

 var templateName = 'Monthly Microsoft Patching'; // Replace with your template name

 // --- Compute 2nd Thursday of this month (instance/user local time) ---
 function two(n) {
     return (n < 10 ? '0' : '') + n;
 }
 var now = new GlideDateTime();
 var year = now.getYearLocalTime(); // e.g., 2026
 var month = now.getMonthLocalTime() + 1; // 1..12 (getMonthLocalTime is 0-based)

 // Set to the first day of the month at 00:00

 var firstOfMonth = new GlideDateTime();
 firstOfMonth.setDisplayValue(year + '-' + two(month) + '-01 00:00:00');

 // Day of week: 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat

 var dow = firstOfMonth.getDayOfWeekLocalTime();
 var THURSDAY = 4;

 // Days to add to reach the first Thursday of the month

 var toFirstThursday = (THURSDAY - dow + 7) % 7;
 var firstThursday = new GlideDateTime(firstOfMonth);
 firstThursday.addDaysLocalTime(toFirstThursday);
 var secondThursday = new GlideDateTime(firstThursday);
 secondThursday.addDaysLocalTime(7);

 var dateOnly = secondThursday.getLocalDate().getValue(); // yyyy-MM-dd
 var change = new GlideRecord('change_request');
 change.initialize();
 change.std_change_producer_version = gs.getProperty('SJRM02022026_std_change_producer_version'); // Standard Change Templates version
 change.applyTemplate('Monthly Microsoft Patching');

 // Insert the change request

 change.chg_model = gs.getProperty('SJRM02022026_chg_model'); // Change model is standard
 change.assignment_group = gs.getProperty('SJRM02022026_chg_assignment_group'); //Assignment Group: Change Management

 // Set Planned Start to the computed 2nd Thursday (UTC value is stored)

 change.setValue('start_date', secondThursday.getValue());

 var changeSysId = change.insert();
 gs.info('Standard change request created: ' + changeSysId + ' | Planned start (2nd Thu): ' + secondThursday.getDisplayValue());

 //Change Task Creation

 var rec = new GlideRecord('change_task');

 rec.initialize();

 rec.change_request = changeSysId;

 rec.short_description = 'Merchandising Quality Engineering Testing';

 rec.description = 'Confirm business application functions as it should with new Microsoft patches installed.';

 rec.assignment_group.setDisplayValue('Merchandising Quality Engineering');

 rec.insert();

View solution in original post

6 REPLIES 6

Chaitanya ILCR
Giga Patron

Hi @Arjun Reddy Yer ,

var templateName = 'Monthly PMPC Patching'; // Replace with your template name
var change = new GlideRecord('change_request');
change.initialize();
change.std_change_producer_version = gs.getProperty('SJRM02022026_std_change_producer_version_pmpc'); // Standard Change Templates version
change.applyTemplate('Monthly PMPC Patching');

// Insert the change request

change.chg_model = gs.getProperty('SJRM02022026_chg_model'); // Change model is standard
change.assignment_group = gs.getProperty('SJRM02022026_chg_assignment_group'); //Assignment Group: Change Management
change.start_date = getPlannedStartDateTimeValue();
var changeSysId = change.insert();
gs.info('Standard change request created: ' + changeSysId);

// Change Task Creation

var rec = new GlideRecord('change_task');
rec.initialize();
rec.change_request = changeSysId;
rec.short_description = 'Merchandising Quality Engineering Testing';
rec.description = 'Confirm business application functions as it should with new Microsoft patches installed.';
rec.assignment_group.setDisplayValue('Merchandising Quality Engineering');
rec.insert();

function nThWeekDayInAMonth(day, num) {
    var date = new GlideDate();
    var currentMonth = date.getMonth();
    var temp = 0;
    for (var i = 0; i <= 7 * num && i <= date.getDaysInMonth(); i++) {
        date.addDays(1);
        if (date.getDayOfWeek() == day)
            temp += 1;
        if (temp == num)
            return date.getDate();

    }
}

function getPlannedStartDateTimeValue() {
    var gd = new GlideDateTime(nThWeekDayInAMonth(4, 2));
    gd.addSeconds(3600); //add you time in seconds here example if you want planned start date to 12:30 PM it will 12*60*60 + 30*60 calucuate and add it in place of 3600 
    return gd.getValue();
}

 

just adjust the gd.addSeconds according to the comment added next to that

 

you do the same with the planned end date clone the function and change the name and give the time in seconds with lets say for 4:30 PM (16*60*60 + 30*60) calculate it and add it

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

It's not working as the Planned Start Date field is not auto populating with the 2nd Thursday of the month

Hi @Arjun Reddy Yer ,

 

I have tried this background script, so you can use/make changes as required:

 

// ===== TEST: Find 2nd Thursday of the current month =====

// Function to find the Nth occurrence of a weekday in the current month
// day = day of week (1=Sunday, 2=Monday, ..., 5=Thursday)
// num = which occurrence (e.g., 2 = second Thursday)
function nThWeekDayInAMonth(day, num) {
var date = new GlideDate();
date.setDayOfMonth(1); // Move to the 1st day of the current month

var count = 0; // Counter for matching weekdays
var daysInMonth = date.getDaysInMonth(); // Total days in this month

// Loop through all days in the month
for (var i = 0; i < daysInMonth; i++) {

// Check if current day matches the requested weekday
if (date.getDayOfWeek() == day) {
count++;

// If this is the Nth occurrence, return the date
if (count == num) {
return date.getValue(); // Returns date in yyyy-MM-dd format
}
}

// Move to the next day
date.addDays(1);
}

// If Nth weekday is not found (should not normally happen), return null
return null;
}

// Function to return a GlideDateTime for the 2nd Thursday with a fixed time
function getPlannedStartDateTimeValue() {

// Get the date (yyyy-MM-dd) for the 2nd Thursday
var dateStr = nThWeekDayInAMonth(5, 2); // 5 = Thursday, 2 = second

// Create a GlideDateTime and set time to 01:00:00
var gd = new GlideDateTime(dateStr + " 01:00:00");

return gd; // Return GlideDateTime object
}

// ===== Run test =====
var result = getPlannedStartDateTimeValue();

// Log output for verification
gs.info("===== TEST OUTPUT =====");
gs.info("2nd Thursday (date only): " + nThWeekDayInAMonth(5, 2));
gs.info("Planned Start DateTime: " + result.getDisplayValue());

 

Mohammed8_1-1770150778425.png

Regards,

Mohammed Zakir

 

It's working with below script thanks for sharing the script

 var templateName = 'Monthly Microsoft Patching'; // Replace with your template name

 // --- Compute 2nd Thursday of this month (instance/user local time) ---
 function two(n) {
     return (n < 10 ? '0' : '') + n;
 }
 var now = new GlideDateTime();
 var year = now.getYearLocalTime(); // e.g., 2026
 var month = now.getMonthLocalTime() + 1; // 1..12 (getMonthLocalTime is 0-based)

 // Set to the first day of the month at 00:00

 var firstOfMonth = new GlideDateTime();
 firstOfMonth.setDisplayValue(year + '-' + two(month) + '-01 00:00:00');

 // Day of week: 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat

 var dow = firstOfMonth.getDayOfWeekLocalTime();
 var THURSDAY = 4;

 // Days to add to reach the first Thursday of the month

 var toFirstThursday = (THURSDAY - dow + 7) % 7;
 var firstThursday = new GlideDateTime(firstOfMonth);
 firstThursday.addDaysLocalTime(toFirstThursday);
 var secondThursday = new GlideDateTime(firstThursday);
 secondThursday.addDaysLocalTime(7);

 var dateOnly = secondThursday.getLocalDate().getValue(); // yyyy-MM-dd
 var change = new GlideRecord('change_request');
 change.initialize();
 change.std_change_producer_version = gs.getProperty('SJRM02022026_std_change_producer_version'); // Standard Change Templates version
 change.applyTemplate('Monthly Microsoft Patching');

 // Insert the change request

 change.chg_model = gs.getProperty('SJRM02022026_chg_model'); // Change model is standard
 change.assignment_group = gs.getProperty('SJRM02022026_chg_assignment_group'); //Assignment Group: Change Management

 // Set Planned Start to the computed 2nd Thursday (UTC value is stored)

 change.setValue('start_date', secondThursday.getValue());

 var changeSysId = change.insert();
 gs.info('Standard change request created: ' + changeSysId + ' | Planned start (2nd Thu): ' + secondThursday.getDisplayValue());

 //Change Task Creation

 var rec = new GlideRecord('change_task');

 rec.initialize();

 rec.change_request = changeSysId;

 rec.short_description = 'Merchandising Quality Engineering Testing';

 rec.description = 'Confirm business application functions as it should with new Microsoft patches installed.';

 rec.assignment_group.setDisplayValue('Merchandising Quality Engineering');

 rec.insert();