To exclude non-business days calculation for additional comments entered by user.

diNesh_M
Tera Contributor

 

Hi Team,

I have a requirement to check if any additional comments were added by a user not older than 3 business days .
And if so the ticket will be considered as escalated ticket .
I wrote below script to check if any additional comments were added or not but I am not sure how to exclude non-business days (Saturday ,Sunday)
For example if an user add additional comments on Friday at 11 am then Saturday and Sunday should not be calculated , till Wednesday 11 am the user will have the time to add his next additional comment in the ticket .
Could you help me with the script to achieve the mentioned requirement ?.

 

 

 

 

var grSec = new GlideRecord(table); //table represent incident
	  grSec.addQuery('sys_id',sys_id); //sys_id represent record sys_id
	  grSec.query();
	  if(grSec.next()){
     if(table=='incident'){
       if(grSec.escalation==1){

		var secjournal=new GlideRecord('sys_journal_field');
		secjournal.addQuery('element_id',sys_id);
        secjournal.addQuery('element','comments');
        secjournal.orderByDesc('sys_created_on');
		secjournal.setLimit(1);
        secjournal.query();
		if(secjournal.next()){

         var diffSec = gs.dateDiff(secjournal.sys_created_on, gs.nowDateTime(), true);
            if (diffSec >259200) // 3 days - 259200
                {
                    secEsc=true;   //secEsc represent the ticket is eligible for second escalation
                }
		}
	   }
	   }
}

 

 

 

 

Thanks in advance

1 ACCEPTED SOLUTION

HIROSHI SATOH
Mega Sage

The script below calculates the duration between two datetime fields while considering the business calendar:

// Business Calendar duration calculation
var startDateTime = new GlideDateTime(current.start_date_time); // Replace with your start date/time field
var endDateTime = new GlideDateTime(current.end_date_time);     // Replace with your end date/time field

// Define the schedule you want to use (e.g., '8e7040c0db43130060d8e9f0dabb3547' is the sys_id of the default 24/7 schedule)
var schedule = new GlideSchedule('8e7040c0db43130060d8e9f0dabb3547'); 

// Calculate the duration considering the business calendar
var duration = schedule.duration(startDateTime, endDateTime);

// Convert duration to human-readable format (days, hours, minutes)
var durationInSeconds = duration.getNumericValue();
var durationInDays = Math.floor(durationInSeconds / 86400);
var durationInHours = Math.floor((durationInSeconds % 86400) / 3600);
var durationInMinutes = Math.floor((durationInSeconds % 3600) / 60);

// Display or store the calculated duration
gs.info('Duration: ' + durationInDays + ' days, ' + durationInHours + ' hours, ' + durationInMinutes + ' minutes');

View solution in original post

4 REPLIES 4

Markus Kraus
Kilo Sage

Hi

Assuming you already have a schedule configured for you business days, you can use the following API to check if your date is within this schedule:

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSche...

 

If you need additional help please let us know.

HIROSHI SATOH
Mega Sage

The script below calculates the duration between two datetime fields while considering the business calendar:

// Business Calendar duration calculation
var startDateTime = new GlideDateTime(current.start_date_time); // Replace with your start date/time field
var endDateTime = new GlideDateTime(current.end_date_time);     // Replace with your end date/time field

// Define the schedule you want to use (e.g., '8e7040c0db43130060d8e9f0dabb3547' is the sys_id of the default 24/7 schedule)
var schedule = new GlideSchedule('8e7040c0db43130060d8e9f0dabb3547'); 

// Calculate the duration considering the business calendar
var duration = schedule.duration(startDateTime, endDateTime);

// Convert duration to human-readable format (days, hours, minutes)
var durationInSeconds = duration.getNumericValue();
var durationInDays = Math.floor(durationInSeconds / 86400);
var durationInHours = Math.floor((durationInSeconds % 86400) / 3600);
var durationInMinutes = Math.floor((durationInSeconds % 3600) / 60);

// Display or store the calculated duration
gs.info('Duration: ' + durationInDays + ' days, ' + durationInHours + ' hours, ' + durationInMinutes + ' minutes');

Hi Hiroshi,

 

Thanks for your valuable  input ,will try the script you mentioned and will keep you posted if it works as expected .

 

Thanks

Hi Hiroshi,

 

That works and thanks a lot for your valuable input.

 

Thanks