Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting wrong Duration days

DubeyN
Mega Expert

Hi,

I am trying to create a business rule to get the duration between two fields. I am using below script to get the duration:

*******************************************************************
stts = current.u_status;
if (stts =='Published')
{
var strt = current.u_tariff_effective_date;
var end = current.u_start_time;
var diff = calcDurationSchedule(strt,end);
gs.addInfoMessage(diff);
current.u_duration = diff;
}

function calcDurationSchedule(strt, end) {

var realStart = strt.getGlideObject();
var realEnd = end.getGlideObject();

gs.addInfoMessage(realStart);
gs.addInfoMessage(realEnd);

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'Weekdays 24 / 7');
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
dur = sched.duration(realStart,realEnd);
return dur;
}
*************************************************************************

Problem : When I make changes to the form, this Business rule get triggered. But I am getting wrong business duration. I have added 3 gs.addInfoMessage to get start date, end date and duration returned. I am getting correct date for Start date (2012-02-08) and End Date (2012-03-19) . But Duration returned is a date "1970-01-01 00:00:00". I am not sure why it returning date instead of number of days. Am I missing anything?

Thanks in advance.

ND

11 REPLIES 11

Not applicable

Ah, you have glide_date fields and not glide_date_time fields.

You need to turn them into GlideDateTime objects, before asking the Schedule duration method for an answer:



function calcDurationSchedule(strt, end) {
var realStart = new GlideDateTime(strt.getGlideObject());
var realEnd = new GlideDateTime(end.getGlideObject());

gs.addInfoMessage(realStart);
gs.addInfoMessage(realEnd);

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '24 x 7');
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
dur = sched.duration(realStart,realEnd);
return dur;
}


DubeyN
Mega Expert

Hi,

Thanks for reply.

I updated the Business rule as per your suggestion. But it is still not working.

Check the output return for new rule.

ND


Not applicable

[edited, to remove the exact thing I had actually already written in code]

if you're still not getting a non-zero answer, also go check your schedule to make sure it really does have some active time periods in it. That's the only other gotcha I can think of.

oh, and make sure you've used your schedule's name. I just noticed I had pasted in the one I was testing with.


DubeyN
Mega Expert

Hi James,

I didn't get what you want me to try?

ND


Not applicable

1. Double-check that you corrected this line appropriately:

I wrote:



schedRec.get('name', '24 x 7');


and you originally wrote:


schedRec.get('name', 'Weekdays 24 / 7');


(but your output gave a sys_id, so you probably *did* do that.)

2. Make sure your schedule is defined correctly.