Convert duration field to hours where 1 day = 8 hours

JJG
Kilo Guru

Hello,

I have a duration field called 'days'. I need to convert the number of days into hours and then set a separate integer field to that number. 1 day should = 8 hours. Example: The user enters 5 days and presses submit, the after insert business rule should set the integer field to 40.

(Please don't throw a bunch of links at me, I have looked everywhere)

1 ACCEPTED SOLUTION

you mentioned days in duration type field ? 

try now, tested with below code .

 

 

 

var gr = new GlideRecord('incident');
gr.get('f144d5002f849090f68d5ff62799b688');

var ab = gr.u_duration.getDisplayValue();

var op = ab.split(' ')[0];

var hr = parseInt(op*8);

gs.print(hr);

 

 

 

 

Edit:

 

View solution in original post

9 REPLIES 9

That is getting me a NaN result:

var ab = current.days.getDisplayValue();
var hr = parseInt(ab * 8);
gs.addInfoMessage(hr);

Bhagyashri Sort
Kilo Guru

Hi

Try below code.

  var hoursCalc = duration/(60*60*1000);                                              

  var businessDays = Math.floor(hoursCalc / 24);                          

  var hoursDiff = duration - businessDays*60*60*24*1000;           

  var durCalc = businessDays*(60*60*12*1000)+hoursDiff;          

  var durToAdd = new GlideDuration(durCalc);       

 

Make it correct or mark it as helpful, if my response helps you.

 

Thanks

Bhagyashri Sorte

                         

Prateek kumar
Mega Sage

Make a before insert BR

current.hours_field = parseInt(current.days_field*8);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

That's not working, I'm not sure that you can multiply the entire duration field by 8

I set the duration field to 10 days

Tried this to test your code:

var test = parseInt(current.days); 
gs.addInfoMessage(test);

Got this result:

1970.0