How to convert integer value to Duration

Angshuman3
Mega Guru

Hi All,

I need to convert integer value into Duration format and capture it in a Field whose type is also Duration.

Scenario :

I have a catalog form, which has the below variables :- 
find_real_file.png

If any value is provided, in the 1st variable and the unit is selected, accordingly, we need to convert the value into a duration type (DD : HH : MM : SS) and return the same duration to another field placed out on sc_cat_item table.

Any leads, how we can get this done will be really helpful.

 

Thanks,
Angshuman

1 ACCEPTED SOLUTION

Hi,

try this

// do your calculation here and get milliseconds

var catItem = current.cat_item.getRefRecord();

var durationSelected = parseInt(current.variables.estimated_duration);
if(current.variables.unit == 'Hours'){
var hourConversion = parseInt(durationSelected*60*60*1000);

catItem.durationField.setDateNumericValue(hourConversion);

catItem.update();


Regards
Ankur

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

View solution in original post

16 REPLIES 16

You can try another solution if it doesn't work then let me know, I will explain mine.

@Angshuman 

Did you get a chance to check on approach suggested by me?

Regards
Ankur

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

It's better to have it in seconds if you are doing this level of calculation.

Try that script in "Run Script" activity.

Hi,

you are on workflow of RITM so you can do this

// do your calculation here and get milliseconds

var catItem = current.cat_item.getRefRecord();

catItem.durationField.setDateNumericValue(millisecondsValue);

catItem.update();

Regards
Ankur

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

Hi Ankur,

I am trying the below script to convert the Hour value to Milliseconds, but, unable to get any retrieve any value.

var durationSelected = current.variables.estimated_duration;
if(current.variables.unit == 'Hours'){
var hourConversion = parseInt(durationSelected*60*60*1000);

when, I am putting logs for the hourConversion, I am not getting anything.