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.

Custom date field duration calculation

panchbhaic
Tera Contributor

I have 3 custom date time fields on incident table. 

field 1- Actual Resolve Time

Field 2- Incident management Engagement Time

field 3- IM Engagement Duration

 

I want to calculate the difference between Actual incident resolved time and incident management engagement time and map the duration in IM Engagement duration.

 

IM Engagement Duration = time between the Incident Engagement Time and the Actual Resolve Time


Any suggestion/script would be highly appreciated.

Regards,

Chetan

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@panchbhaic 

you can use this sample script and add in any of your server side code

var plannedDatetime = new GlideDateTime(current.getValue('start_date'));
var actualDatetime = new GlideDateTime(current.getValue('work_start'));
var dur = new GlideDuration();
dur = GlideDateTime.subtract(plannedDatetime, actualDatetime); //the difference between 
current.durationField = dur;

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

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

HI Ankur,
I tried this script but it's not working.


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

    // Add your code here

    var plannedDatetime = new GlideDateTime(current.getValue('u_actual_incident_resolve_time'));
    var actualDatetime = new GlideDateTime(current.getValue('u_incident_management_engagement_time'));
    var dur = new GlideDuration();
    dur = GlideDateTime.subtract(plannedDatetime, actualDatetime); //the difference between
    current.u_im_engagement_duration = dur;


})(current, previous);