Undefined value from Script Include return

arielgritti
Mega Sage

Hi Community

I created a script include to calculate "real-time duration" taking advantage of GlideSchedule() to exclude weekend days and holidays.

I used a typical workday schedule excluding holidays.

I tried the code in a fix script and works. But when  I called the Script Include with the same "logic" the "return" is undefined.

Can you help me?

 

My fix script to test

var startDate = new GlideDateTime('2021-04-01 01:00:00'); //to test include some holiday
var endDate = new GlideDateTime('2021-04-08 03:12:00'); //to test include some holiday and different time

var scheduleID = 'a65583aadb0ba4d4047251f4f39619a2'; //24x5 schedule excluding holidays

//Without ScriptInclude call
var sched = new GlideSchedule();
sched.load(scheduleID); //sys_id of the schedule
var duration = sched.duration(startDate, endDate);
var duration2 = new GlideDuration(duration);
gs.log(duration.getDurationValue());
gs.log(duration2.getDisplayValue());

//With ScriptInclude call
var duration = new AGCaseRealTimeDuration();
duration.getRealTimeDuration(startDate,endDate,scheduleID);
gs.log('AGCaseRealTimeDuration - Start Date: ' + startDate);
gs.log('AGCaseRealTimeDuration -   End Date: ' + endDate);
gs.log('AGCaseRealTimeDuration -   Schedule: ' + scheduleID);
gs.log('AGCaseRealTimeDuration -   Duration: ' + duration.getDurationValue());

 

My script include code

var AGCaseRealTimeDuration = Class.create();
AGCaseRealTimeDuration.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getRealTimeDuration: function(startDate, endDate, scheduleID) {

        var schedule = new GlideSchedule();
		schedule.load(scheduleID); //load the schedule with the holidays exclusion
        var duration = schedule.duration(startDate, endDate);
        gs.log('AGCaseRealTimeDuration Script Include - Duration: ' + duration.getDurationValue());
        return duration;
    },

    type: 'AGCaseRealTimeDuration'
});

 

The results from fix script

Without Scrit include call
*** Script: 3 02:12:00
*** Script: 3 Days 2 Hours 12 Minutes

With Script include call
*** Script: AGCaseRealTimeDuration Script Include - Duration: 3 02:12:00
*** Script: AGCaseRealTimeDuration Script Include - Duration2: 3 02:12:00
*** Script: AGCaseRealTimeDuration - Start Date: 2021-04-01 01:00:00
*** Script: AGCaseRealTimeDuration -   End Date: 2021-04-08 03:12:00
*** Script: AGCaseRealTimeDuration -   Schedule: a65583aadb0ba4d4047251f4f39619a2
*** Script: AGCaseRealTimeDuration -   Duration: undefined
[0:00:00.028] Total Time

 

The log from Script include. You can see is working but when I return the value and try to use in the fix script (and in the business rule where I want to use definitively the value is "undefined")

find_real_file.png

 

The schedule.duration method return a GlideDuration object https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_GlideDurationScopedAPI#r_ScopedGlideDurationGlideDuration_GlideDuration

 

Can you help me? Any idea?

Thanks,

Ariel

 

1 ACCEPTED SOLUTION

Dan Ellis
Kilo Sage

When calling the Script Include, you need to assign the response to a variable so try this:

var duration = new AGCaseRealTimeDuration();
var durationResponse = duration.getRealTimeDuration(startDate,endDate,scheduleID);
gs.log('AGCaseRealTimeDuration - Start Date: ' + startDate);
gs.log('AGCaseRealTimeDuration -   End Date: ' + endDate);
gs.log('AGCaseRealTimeDuration -   Schedule: ' + scheduleID);
gs.log('AGCaseRealTimeDuration -   Duration: ' + durationResponse.getDurationValue());

If that does not work then an alternative would be to just return the duration value rather than the duration object.

Hope this helps!

Thanks,
Dan

View solution in original post

2 REPLIES 2

Dan Ellis
Kilo Sage

When calling the Script Include, you need to assign the response to a variable so try this:

var duration = new AGCaseRealTimeDuration();
var durationResponse = duration.getRealTimeDuration(startDate,endDate,scheduleID);
gs.log('AGCaseRealTimeDuration - Start Date: ' + startDate);
gs.log('AGCaseRealTimeDuration -   End Date: ' + endDate);
gs.log('AGCaseRealTimeDuration -   Schedule: ' + scheduleID);
gs.log('AGCaseRealTimeDuration -   Duration: ' + durationResponse.getDurationValue());

If that does not work then an alternative would be to just return the duration value rather than the duration object.

Hope this helps!

Thanks,
Dan

Hi Dan

Thanks mate! Works fine!

Ariel