Incidents Updated 3 business days ago

pdom
Mega Expert

Hello,

I have to check if an Incident last update date is 3 business days ago, then close the record. The OOTB business rules only checks for 3 calendar days.

I am working below background script to check if Incident update is 3 business days old, but it is not giving positive result. Please point me to right direction

I have tried two different approaches, neither of it working

Approach 1:

var gr = new GlideRecord('incident');

gr.addQuery = ('number','INC0010406');

gr.query();

var dc = new DurationCalculator();

var scheduleSysId = gs.getProperty('sag.businesshour.schedule');

dc.setSchedule('ScheduleSysId'); // this is the sys_id of 8-6 weekday schedule in your instance.

  dc.setTimeZone('GMT');

  var dur = dc.calcScheduleDuration(gs.nowDateTime(),gr.sys_updated_on)/3600;

if(dur>30){ //each working day is 10 hrs, so 3 working days is 10*3 30 hrs

gs.print("duartion is greater than 3 days");

}else gs.print("duration is less than 3 days");

Approach 2 :

// Get the current date/time in correct format for duration calculation

var currentDateTime = new GlideDateTime();

var updatedon ='';

var gr = new GlideRecord('incident');

gr.addQuery = ('number','INC0010406');

gr.query();

updatedon = gr.sys_updated_on.toString();

var startDate = new GlideDateTime('updatedon');

var endDate = new GlideDateTime('currentDateTime');

var schedule = new GlideSchedule();

schedule.load('sag.businesshour.schedule'); // loads "8-5 weekdays excluding holidays" schedule

var duration = schedule.duration(startDate, endDate);

gs.print(duration.getDurationValue()); // gets the elapsed time in schedule

11 REPLIES 11

anaghas
Tera Contributor

Hi,



Did anyone find a solution for the business days calculation. Having a similar requirement with no solution so far:(


Hi Anagha Sundar



Use the below code,



var numberOfDays = 3;


var daysConverted = parseInt(numberOfDays);



var gr = new GlideRecord('incident');


gr.addQuery('incident_state', '10');


gr.addQuery('category' , 'Applications');


gr.addQuery('sys_updated_on', '>', gs.daysAgoStart(daysConverted)); // use '< 'for After condition


......


.....


.....




Please let me know if have any issues.



Thanks


Prici


anaghas
Tera Contributor

Hi Prici,



Thanks for the code. However, it doesn't check for the 3 business days rather just 3 days. Correct me if I'm wrong. My requirement is to check for 3 business days.



Appreciate your help.



Thanks!


Hi Anagha Sundar



Yes you are right, it will just check for 3 days & not business days. You can use Schedules.



find_real_file.png



This is a default schedule, if you want you can create your own schedule with 24 hrs.



Script with schedule,



.....


.....


var updatedTime = gr.sys_updated_on; // incident updated time.



var dc = new DurationCalculator();


dc.setSchedule('<8752763bdbfccf00cb005c98dc961990>');   // Use your instance's weekdays schedule sys_id here


var dur = dc.calcScheduleDuration(updatedTime, gs.nowDateTime());



var convertedDays = (dur/3600); //   result format is Hours .


//var convertedDays = (dur/86400); // result format is days



Please let me know if you need more details.




Thanks


Prici


You can also use as a schedule job and run it daily as below :


var gr = new GlideRecord('incident');


gr.addActiveQuery();


gr.query();


while(gr.next())


{


var date1= new GlideDateTime(gr.sys_updated_on.toString()); //2017-12-28 01:30:30


var date2= new GlideDateTime();


var sc = new GlideSchedule();


sc.load('08fcd0830a0a0b2600079f56b1adb9ae'); // Schedule sys_id you need to load for 8-5 weekdays.


var duration = sc.duration(date1, date2);


var dr = duration.getDurationValue(); // gets the duration time in DD HH:mm:ss format


var day = new GlideDuration(dr);


if(day.getDayPart() == 3)


{


// For closing as oob incident


gr.incident_state = '7';


gr.close_code = 'Solved (Work Around)';


gr.close_notes = 'Test';


}


}