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();
3 REPLIES 3

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

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