Calculate duration based on schedule(Using Business Rule)

padhu1
Kilo Expert

I need to be able to calculate the difference between two date fields in days based on a schedule using Business Rule (business days),.   E.g. If start date is 01/03/2014 and end date is 08/03/2014, I want this answer to be 5 days (as 2 days out of the 7 are weekend days ).

There is a mention of this on the Wiki but it talks about packages.com and doesn't seem to work.

I have also seen 'GlideSchedule' referenced but not how to use it.

Has anyone got any thoughts please? Your help would be much appreciated.

4 REPLIES 4

dvp
Mega Sage
Mega Sage

Take a look at this discussion.


Business Duration Calculation




if your instance is either on Calgary or above   releases then you don't need to worry about packages as SN supporting these functionalities using APIs


Abhinay Erra
Giga Sage

Use GlideSchedule API. Your code will look like this



var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //sys_id of your schedule goes here


  // Get duration based on schedule/timezone


  var dur= (sched.duration(new GlideDateTime(<start date>), new GlideDateTime(<end date>)).getDurationValue());


I tried your approach below s my code but it gives me wrong output.



(function executeRule(current, previous /*null when async*/) {




// Add your code here


var startDateTime =new GlideDateTime(current.start_date);


var endDateTime =new GlideDateTime(current.end_date);


var sched =new GlideSchedule('sys_id', '08fcd0830a0a0b2600079f56b1adb9ae'); //8-5 weekdays


var dur= (sched.duration(startDateTime,endDateTime ).getDurationValue());


gs.log('the t time is'+dur , 'datevalidation');


})(current, previous);




Output is the t time is00:00:00




preddy
Kilo Guru

Hi Pardhu,



You can find the diiference between two dates using schedule.



1.create business rule on between two dates.



2. call that business rule in script Actions.