Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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

AshishKM
Kilo Patron

Hi @Arjun Reddy Yer , 

Logically we need to find the first Thursday of the month and then add "7" to get the second one using addDaysUTC(7) method.

 

 

-Thanks,

AshishKM

 

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Ankur Bawiskar
Tera Patron

@Arjun Reddy Yer 

so what's not working and where are you stuck?

 

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader