Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

MR1
Tera Contributor

We don't have any close date field.

Just we need populate the Agent duration of the interaction record when (sys_created_on)created datetime is more than current datetime.

Thanks

When you say sys_created_on is more than current date time, what do you mean?
Is it possible to have created date being greater than current time?

var currentDate = new GlideDateTime(gs.nowDateTime());
var openedAt = new GlideDateTime(current.getValue("sys_created_on"));
return new GlideDuration(GlideDateTime.subtract(openedAt, currentDate));
   
Best Regards
Aman Kumar