- 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-03-2019 05:03 AM
I just copied and pasted the script above in to scripts background on to my Madrid instance and called the function and it worked fine. No errors.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 06:03 AM
Hi Chuck,
Thanks for the response. I have also tried running this as a background script and it works correctly. It seems the issue comes from attempting to run it as a Business Rule. I have attached a screenshot of the error. I can't seem to find any mentions of this exact error being reproduced anywhere on community or elsewhere. Will likely end up raising a Hi call.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 08:13 AM
Can you provide more details/screenshots about the business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 08:36 AM
I moved the logic of the business rule into a script include but the same error persists. It's also maybe worth me noting that I am working on a domain separated instance.
Business Rule Details
- When: before (have also tried 'after')
- Order: 100
- Advanced: true
- Runs on: Insert
- No filter or role conditions
- No actions
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var newInclude = new CABApprovalRequired();
newInclude.initialize(current.start_date, current.end_date);
if (newInclude.checkSchedule()) {
gs.addInfoMessage('Within Schedule. CAB Required.');
current.cab_required();
} else {
gs.addInfoMessage('Not within schedule. No CAB required');
}
})(current, previous);
Script Include
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 sched;
var grSchedule = new GlideRecord('cmn_schedule');
grSchedule.addQuery('name','8-5 weekdays');
grSchedule.query();
while (grSchedule.next()) {
sched = new GlideSchedule(grSchedule.sys_id);
}
if (sched.isInSchedule(this.start_time) || sched.isInSchedule(this.end_time)) {
return true;
}
} catch (err) {
this.getErrorMsg(err);
}
},
type: 'CABApprovalRequired'
};
Thanks 🙂