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.

Add two duration fields together and populate custom field with calculation BR

Cheyenne1
Kilo Guru

Good morning, 

I'm trying to add two duration fields together on Incident. 

I have a custom field called u_incident_total_time_worked which is pulling data from the Incident Tasks time worked (total duration spent on all Incident tasks) - working fine. 

I have the OOTB field time_worked on Incident which is required on resolve, at which point on update/save I would like to populate my custom total_time_worked field on Incident. 

 

so basically I want:

total_time_worked = incident_task_total_time_worked + time_worked

 

My before BR looks like this ( but it looks like it's pulling non human time from somewhere) which is triggered when time_worked changes.

 

var inctasktotal = new GlideDuration(current.u_incident_task_total_time_worked).getNumericValue();
var timeworked = new GlideDuration(current.time_worked).getNumericValue();
var total = inctasktotal + timeworked;
var totaltimeworked= new GlideDuration();

totaltimeworked.setNumericValue(total);

current.u_total_time_worked = totaltimeworked.getDurationValue();

 

All help is appreciated 🙂 

7 REPLIES 7

Hey, I had made the bracket adjustment previous and it didn't work. The business rule sets the field blank for some reason. 

 

I had a value in the total time worked field and it blew it out on update. hmmm 

ggg
Giga Guru

I used 2 fields on incident of type duration= for my example, business_duration and calendar_duration.

when you retrieve those fields in a business rule they are not GlideDuration objects.

you have to get the value, create a string in the format 'D HH:mm:ss'

and instantiate a GlideDuration object with the string in your br.

you can then add them.

I did string manipulation to get the STring value in the format I need.

you can run this in FixScript to test.

If anyone knows of an easier way to do this please chime in.

here is my code:

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0000059');
gr.query();
if (gr.next()){
    
    //*************************************************************
    var bus_dur = gr.getDisplayValue('business_duration');
    gs.print('bus dur display value ' + bus_dur); //1577 Days 13 Hours
    gs.print(typeof bus_dur); //string
    
    var dayIndex = bus_dur.indexOf('Days');
    gs.print(dayIndex); //5
    
    var dayPart = bus_dur.substring(0, dayIndex);
    gs.print(' day part is ' + dayPart);
    
    var hourIndex = bus_dur.indexOf('Hours');
    gs.print(hourIndex); //5
    
    var startHourIndex = dayIndex + 5;
    gs.print('start Hour Index is ' + startHourIndex);
    var hourPart = bus_dur.substring(startHourIndex, hourIndex);
    gs.print('hour part is ' + hourPart);
    
    var newString = dayPart + trim(hourPart) + ':00:00';
    var dur1 = new GlideDuration(newString);
    gs.print('new string is ' + newString);
    gs.print('');
    gs.print('************************************************');
    gs.print('');
    //*************************************************************
    var cal_dur = gr.getDisplayValue('calendar_duration');
    gs.print('cal dur display value ' + cal_dur); //1577 Days 13 Hours
    gs.print(typeof cal_dur); //string
    
    dayIndex = cal_dur.indexOf('Days');
    gs.print(dayIndex); //5
    
    dayPart = cal_dur.substring(0, dayIndex);
    gs.print(' day part is ' + dayPart);
    
    hourIndex = cal_dur.indexOf('Hours');
    gs.print(hourIndex); //5
    
    startHourIndex = dayIndex + 5;
    gs.print('start Hour Index is ' + startHourIndex);
    hourPart = cal_dur.substring(startHourIndex, hourIndex);
    gs.print('hour part is ' + hourPart);
    
    newString = dayPart + trim(hourPart) + ':00:00';
    var dur2 = new GlideDuration(newString);
    gs.print('new string is ' + newString);
    
    //*************************************************************
    
    var a = dur1.add(dur2);
    gs.print('sum is ' + a.getDisplayValue());
    
    
}

Cheyenne1
Kilo Guru

So I just realized time_worked OOTB on Incident is a timer, not a duration... I think I need to query the task_time_worked duration field then?