
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 08:41 AM
Our system is running 4 environments; DEV -> TEST -> STAGE -> PROD
Per a request from an end-user;
We would like to auto-generate [a standard change] every Monday, which will be scheduled to start on Friday at 11 PM, and will be scheduled to complete on Saturday, at 8 AM.
I created the following system in the DEV environment:
RepeatingStandardChange Script Includes:
Application: Global
Accessible from: This application scope only
API Name: global.RepeatingStandardChange
Client callable: false
Active: true
Protection policy: --None--
Script:
var RepeatingStandardChange = Class.create();
RepeatingStandardChange.prototype = {
initialize: function() {
},
/*
Given the following params are set before execution:
template [string]: the name of the CHG template
assignUser [string]: the username of the user to assign the CHG to
assignGroup [string]: the group name to assign the CHG to
shortDesc [string]: the short description for the CHG
desc [string]: the description for the CHG
startDateTime [string]: (YYYY-MM-DD hh:mm:ss format) the start date for the CHG
endDateTime [string]: (YYYY-MM-DD hh:mm:ss format) the end date for the CHG
Create a new CHG, and return the number
*/
createWithTemplate: function(){
//Lookup the provided template
var templateGr = new GlideRecord('std_change_producer_version');
templateGr.addQuery('std_change_producer.name', '=', this.template);
templateGr.orderByDesc('version');
templateGr.query();
//If the template was found
if(templateGr.next()){
//Initialize a new change
var chgGr = new GlideRecord('change_request');
chgGr.initialize();
//Standard Type
chgGr.type = 'standard';
chgGr.std_change_producer_version = templateGr.sys_ID;
//Apply query from template
chgGr.applyEncodedQuery(templateGr.std_change_producer.template.template);
//Compile dates into GDT objects
var start = new GlideDateTime;
if(this.startDateTime != '' && this.startDateTime != undefined) start.setDisplayValue(this.startDateTime);
var end = new GlideDateTime;
if(this.endDateTime != '' && this.endDateTime != undefined) end.setDisplayValue(this.endDateTime);
//Set dates
chgGr.work_start = start.getDisplayValue();
chgGr.work_end = end.getDisplayValue();
chgGr.start_date = start.getDisplayValue();
chgGr.end_date = end.getDisplayValue();
//Set assigned user
var userGr = new GlideRecord('sys_user');
userGr.addQuery('user_name', this.assignUser);
userGr.query();
if(userGr.next()) chgGr.assigned_to = userGr.sys_id.toString();
else return "User provided for assignment was not found.";
//Set assignment group
var groupGr = new GlideRecord('sys_user_group');
groupGr.addQuery('name', this.assignGroup);
groupGr.query();
if(groupGr.next()) chgGr.assignment_group = groupGr.sys_id.toString();
else return "Group provided for assignment was not found.";
//Set description
chgGr.short_description = this.shortDesc;
//Replace newlines with html breaks
chgGr.description = this.desc.replace(/\n/g, "<br />");
//Create the change
var newChange = chgGr.insert();
//Manually move through the steps to complete the change
var chg = new GlideRecord('change_request');
chg.get(newChange);
chg.state = -2;
chg.update();
chg.state = -1;
chg.update();
chg.state = 0;
chg.update();
chg.close_code = 'successful';
chg.state = 3;
chg.update();
//Return the CHG number
return chg.number.toString();
}
else{
return "!!! Template provided was not found.";
}
},
type: 'RepeatingStandardChange'
};
Scheduled Job:
Name: Standard Change: [Standard Change Name]
Active: true
Run: Weekly
Day: Monday
Time: 8 Hours 00 Minutes 00 Seconds
Conditional: false
Script:
var chgUtil = new RepeatingStandardChange;
//Short description for the CHG
chgUtil.shortDesc = 'Centerstone: Maintenance/Patch Deploy';
//Description for the CHG
chgUtil.desc = '[...]'
//Assigned to user
chgUtil.assignUser = '[user for assignment]';
//Assignment group
chgUtil.assignGroup = '[group for assignment]';
//CHG template name
chgUtil.template = '[Change Template to use]';
//"Scheduled for Friday at 11PM to Saturday at 8AM"
//Monday at 8 AM
var now = new GlideDateTime();
var fridayNight = new GlideDateTime();
//Move to friday
now.addDaysUTC('4');
//Move to 11 PM
now.addSeconds(60*60*15);
fridayNight.setDisplayValue(now.getDisplayValue());
var saturdayMorning = new GlideDateTime();
saturdayMorning.setDisplayValue(fridayNight.getDisplayValue());
//Move ahead 9 hours
saturdayMorning.addSeconds(60*60*9);
//Set date values on the SI Object
chgUtil.startDateTime = fridayNight;
chgUtil.endDateTime = saturdayMorning;
//Create the CHG
var chgNum = chgUtil.createWithTemplate();
if(chgNum != '' && chgNum != undefined) gs.log('[Change Name] CHG created: ' + chgNum);
else gs.log('[Change Name] CHG creation failed!');
And in the DEV environment, this worked exactly as I expected it to (the times being displayed are in UTC, vs EST, however the important component is the fact that the times are there):Given this succeeded, I moved both the Script Includes, and the Scheduled Job to the TEST environment, however once the Update Set was applied, and the scheduled job ran, I noticed that the Planned start date and Planned end date were not configured correctly:
They are both generating on the correct day, but not at the correct time. I have verified that both the Script Includes, as well as the Scheduled Job are exactly the same in both the DEV and TEST environments.
This same strange behavior is also observed in the STAGE, and PROD environments, as well.
After reaching out to HI, and creating a ticket, they were unable to determine the cause of this issue.
My hope is that someone has some insight into what is causing this.
Solved! Go to Solution.
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 09:45 AM
Hi,
I can't tell why the times would differ in your instances if the code is exactly the same, but I can give some pointers on your code.
In many places you should replace the assignment with appropriate setValue method, as is best practice.
example:
// replace this
chgGr.work_start = start.getDisplayValue();
chgGr.work_end = end.getDisplayValue();
chgGr.start_date = start.getDisplayValue();
chgGr.end_date = end.getDisplayValue();
// with this
chgGr.setValue('work_start', start.getDisplayValue());
chgGr.setValue('work_end', end.getDisplayValue());
chgGr.setValue('start_date', start.getDisplayValue());
chgGr.setValue('end_date', end.getDisplayValue());
You could try to pass the start and end datetime as variables to your function like this
chgUtil.createWithTemplate(startTime, endTime);
Have you done some debugging as to find where the time goes wrong?
Finally, you could consider creating a Flow, instead of a scheduled job, it would do the same thing, but would probably not require any coding.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 09:45 AM
Hi,
I can't tell why the times would differ in your instances if the code is exactly the same, but I can give some pointers on your code.
In many places you should replace the assignment with appropriate setValue method, as is best practice.
example:
// replace this
chgGr.work_start = start.getDisplayValue();
chgGr.work_end = end.getDisplayValue();
chgGr.start_date = start.getDisplayValue();
chgGr.end_date = end.getDisplayValue();
// with this
chgGr.setValue('work_start', start.getDisplayValue());
chgGr.setValue('work_end', end.getDisplayValue());
chgGr.setValue('start_date', start.getDisplayValue());
chgGr.setValue('end_date', end.getDisplayValue());
You could try to pass the start and end datetime as variables to your function like this
chgUtil.createWithTemplate(startTime, endTime);
Have you done some debugging as to find where the time goes wrong?
Finally, you could consider creating a Flow, instead of a scheduled job, it would do the same thing, but would probably not require any coding.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2022 07:54 AM
Thanks for the assist - after changing to method calls to set values, as well as passing in Display Values instead of raw GlideDateTime objects, the issue seems to be resolved.