How to get the time in along with the date in glide duration in the business rule.

Devi12
Giga Guru

Hi,

There is a business rule written on "sn_vul_vulnerable_item" table to calculate the business days. Below is the script for the same.

Devi12_0-1684474112503.png

It is an onBefore business rule. Currently it is calculating the target date only based on the field "last_opened_dt_tm". My requirement is to calculate the time also along with the date. How can I do that?

11 REPLIES 11

I have used the below script as well, but not working.

 

(function executeRule(current, previous /*null when async*/ ) {
 
    //Fetching last Opened Date
    var lastOpenedDate =new GlideDateTime(current.last_opened_dt_tm); 
 
//Adding business days
    //var date = new GlideDateTime(lastOpenedDate.getDate());
var date = new GlideDateTime();
var days=parseInt(current.ttr_applied_rule.ttr_max);
//var gtime1 = new GlideTime();
//gtime1=parseInt(current.ttr_applied_rule.ttr_max);
//gs.info('laxmi date1 ', +current.ttr_applied_rule.ttr_max);
 
    var dur = new GlideDuration((60 * 60 * 8 * 1000 * days));
   
var schedule = new GlideSchedule('82fba44e877e655098264b59dabb35dd');
 
    var newDateTime = schedule.add(date, dur);
    var newDate = newDateTime.getDate();
 
//Updating new target date
var remediationDate=new GlideDateTime(newDate);
current.ttr_target_date=remediationDate;
gs.info('test rMT1', + current.ttr_target_date);
 
 
})(current, previous);

@Devi12 

small update in the script I shared earlier.

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

//Fetching last Opened Date
var lastOpenedDate = new GlideDateTime(current.last_opened_dt_tm);

//Adding business days
var date = new GlideDateTime();
var days = parseInt(current.ttr_applied_rule.ttr_max);
var dur = new GlideDuration((60 * 60 * 8 * 1000 * days));
var schedule = new GlideSchedule('82fba44e877e655098264b59dabb35dd');
var newDateTime = schedule.add(date, dur);

//Updating new target date
current.ttr_target_date = newDateTime;

})(current, previous);

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Tried that, didn't calculated date.

Devi12_0-1684489218153.png

 

@Devi12 

what's your schedule? how many hours are there in it?

Did you manually check what time you get by adding those days to the start date?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Below is the schedule:

Devi12_0-1684491889277.png

The date is calculated correctly based on the field "last_opened_dt_tm", but it is not calculating the time. Some time the time is taken as '2023-06-01 17:00:00' for last opened date "2023-05-19 00:00:00"and '2023-05-29  00:00:00' for last opened date "2023-05-19 00:00:00" as shown in above attachment.