Getting wrong Duration days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2012 07:51 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2012 02:45 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2012 06:41 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2012 07:23 AM
[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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2012 07:25 AM
Hi James,
I didn't get what you want me to try?
ND

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2012 07:51 AM
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.