Can't find method com.glide.schedules.Schedule.isInSchedule

Ethan Davies
Mega Sage
Mega Sage

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

 

Error running business rule 'BR Name' on change_request:CHG0030050, exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.schedules.Schedule.isInSchedule(java.lang.String). (sys_script.d88fb97ddb7df3802ffd983cca961982.script; line 19)
 
I have referred to the official documentation to see if the method I am trying to call still exists, and as of Madrid (current instance family) I can't see any mention of this method being deprecated.
 
Any pointers would be greatly appreciated. See my code below. 
 
Thanks,
Ethan
 
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;
	}
	}
}
1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

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'
};

View solution in original post

11 REPLIES 11

Poonam Mahajan
Tera Contributor

Hello,

I am using the same script as given,

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;
				}

 Passed schedule sys id inside the GlideSchedule but its not going inside the if block.

 

Can you please help me with that.

Thanks 

We need to calculate the duration between these start and end below is my code snippet,

var taskCreated = grTask.sys_created_on;
var firstUpdated = grTask.work_start;

var schedObj = new GlideSchedule('1b499f9cf78e6d105ef541b84851e048');
var start = new GlideDateTime(taskCreated.getDisplayValue());
var end = new GlideDateTime(firstUpdated.getDisplayValue());


if (schedObj.isInSchedule(start) || schedObj.isInSchedule(end)) {

var duration = schedObj.duration(start ,end);
gs.info("dur-->"+duration);

}

 

But its not going inside if block also its giving wrong duration.

Can you please help me with that.

 

Thanks