- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2019 04:18 AM
Hi All,
I am trying to write a Business Rule that compares to the Planned Start Date on a new Change Request. If this Planned Start Date falls within the span of a given schedule then I am marking the Change Request to require CAB approval.
I was under the impression that I would be able to achieve this using the GlideSchedule.isInSchedule method, but the Business Rule throws up an error stating
function checkWithinSchedule () {
var g = new GlideRecord('cmn_schedule');
g.addQuery('name', '8-5 weekdays');
g.query();
if (g.next()) {
var sched = new GlideSchedule(g.sys_id);
var d = new GlideDateTime();
d.setDisplayValue("2019-06-03 12:00:00");
if (sched.isInSchedule(d)) {
gs.info("Is in the schedule");
return true;
} else {
gs.info("Is NOT in the schedule");
return false;
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 06:01 AM
Found it! I converted this.start_time/end_time to GlideDateTime() and passed that to isInSchedule()... No more error.
var CABApprovalRequired = Class.create();
CABApprovalRequired.prototype = {
initialize: function(start_time, end_time) {
this.start_time = start_time;
this.end_time = end_time;
},
getErrorMsg: function (err) {
// gs.addErrorMessage('Error: '+ current.name);
gs.addErrorMessage('Error: ' + err);
},
checkSchedule : function() {
try {
var schedGr = new GlideRecord('cmn_schedule');
schedGr.addQuery('name','8-5 weekdays');
schedGr.query();
gs.info(schedGr.getRowCount() + ' rows');
if (schedGr.next()) {
var schedObj = new GlideSchedule(schedGr.sys_id);
var start = new GlideDateTime(this.start_time.getDisplayValue());
var end = new GlideDateTime(this.end_time.getDisplayValue());
if (schedObj.isInSchedule(start) || schedObj.isInSchedule(end)) {
return true;
}
}
return false;
} catch (err) {
this.getErrorMsg(err);
}
},
type: 'CABApprovalRequired'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 04:29 AM
A couple things:
What table is this BR running against? (this should also tell me if it's scoped or global)
What is current.cab_required()
You don't need to call initialize() from your script include explicitly. Put the two parameters in the instantiation. Ex:
var newInclude = new CABApprovalRequired(current.start_date, current.end_date);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 04:45 AM
The BR is running against the change_request table and it's global
current.cab_required() is an error on my part, this has been changed to
current.cab_required = true;
And yes, thanks for the pointer on the script include.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 04:51 AM
As I started working on this, I also noticed you left current.name in your getErrorMsg() function. current is not known in the script include. It's there as an artifact from your BR, but that means you cannot unit test this from outside the BR (e.g. in scripts background for example)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 06:01 AM
Found it! I converted this.start_time/end_time to GlideDateTime() and passed that to isInSchedule()... No more error.
var CABApprovalRequired = Class.create();
CABApprovalRequired.prototype = {
initialize: function(start_time, end_time) {
this.start_time = start_time;
this.end_time = end_time;
},
getErrorMsg: function (err) {
// gs.addErrorMessage('Error: '+ current.name);
gs.addErrorMessage('Error: ' + err);
},
checkSchedule : function() {
try {
var schedGr = new GlideRecord('cmn_schedule');
schedGr.addQuery('name','8-5 weekdays');
schedGr.query();
gs.info(schedGr.getRowCount() + ' rows');
if (schedGr.next()) {
var schedObj = new GlideSchedule(schedGr.sys_id);
var start = new GlideDateTime(this.start_time.getDisplayValue());
var end = new GlideDateTime(this.end_time.getDisplayValue());
if (schedObj.isInSchedule(start) || schedObj.isInSchedule(end)) {
return true;
}
}
return false;
} catch (err) {
this.getErrorMsg(err);
}
},
type: 'CABApprovalRequired'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2019 08:17 AM
Great! Thank you Chuck 🙂