Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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

Ankur Bawiskar
Tera Patron

@Devi12 

then you need to have date/time field to be populated.

please share script here.

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

Hi Ankur,

Below is the existing script:

 

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 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);
    var newDate = newDateTime.getDate();
 
//Updating new target date
var remediationDate=new GlideDateTime(newDate);
current.ttr_target_date=remediationDate;
 
Devi12_3-1684481849292.png

 

@Devi12 

you are already using schedule so it will add based on it and will give you output in time

update script as this

(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 = remediationDate;

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

Thanks for the response!

I tried your script not working. I can see in logs "com.glide.script.RhinoEcmaError: "remediationDate" is not defined."