Calculation the duration (time) between the interaction created and current time.

MR1
Tera Contributor

Hi All,

I have a custom field 'u_Agent_duration'(duration type) and it's on the Interaction table. 
I would like to calculate the difference between created datetime and current datetime. Then populate the Agent duration field with the duration difference 

I wrote this below BR on interaction table on After, Insert and update checked. 

Unfortunately, it is not working.

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

	// Add your code here

	var creationTime = new GlideDateTime(current.sys_created_on);
	var nowTime = new GlideDateTime();
	var duration = GlideDateTime.subtract(creationTime, nowTime);
	current.u_Agent_duration = duration; // give proper field name here

})(current, previous);


1 ACCEPTED SOLUTION

Hi,

so after 30mins you want the duration to be set?

then you can add 30mins to your now time and then get duration

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

    // Add your code here
    var creationTime = new GlideDateTime(current.sys_created_on);
    var nowTime = new GlideDateTime();
    nowTime.addSeconds(1800); // add 30mins i.e. 1800 seconds
    var duration = GlideDateTime.subtract(creationTime, nowTime);
    current.u_Agent_duration = duration; // give proper field name here

})(current, previous);

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

BR should be before insert/update

Regards
Ankur

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

Thanks for your quick response Ankur!

Could you please help me to add condition, so that, BR should populate the custom field soon after the 30 minutes.

Or do we need a scheduled job to do this?

Hi,

so after 30mins you want the duration to be set?

then you can add 30mins to your now time and then get duration

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

    // Add your code here
    var creationTime = new GlideDateTime(current.sys_created_on);
    var nowTime = new GlideDateTime();
    nowTime.addSeconds(1800); // add 30mins i.e. 1800 seconds
    var duration = GlideDateTime.subtract(creationTime, nowTime);
    current.u_Agent_duration = duration; // give proper field name here

})(current, previous);

Regards
Ankur

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

Aman Kumar S
Kilo Patron

Hey,

I think you can populate this information using calculate value in dictionary level:

var currentDate = new GlideDateTime(gs.nowDateTime());
    var closedAt = new GlideDateTime(current.getValue("closed_at"));
    var openedAt = new GlideDateTime(current.getValue("opened_at"));
    var dur;
    if (current.getValue("active") == true) {
        
        dur = new GlideDuration(GlideDateTime.subtract(openedAt, currentDate));
    } else {
        
        dur = new GlideDuration(GlideDateTime.subtract(openedAt, closedAt));
    }

    return dur;
Best Regards
Aman Kumar