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

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

Thanks Ankur, this did work, I am providing you the Run Script code which I am building. 

For Months Week Days, do I have to follow the same pattern as you have suggested?

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

var gr = new GlideRecord('sc_cat_item');
gr.initialize();
gr.name = current.variables.name;
gr.short_description = current.variables.short_description;
gr.description = current.variables.description;
gr.active = 'false';
gr.u_est_handle_time.setDateNumericValue(hourConversion);
gr.insert();

Thanks to informing that my script is working.

it would be on similar line

Just ensure you convert properly based on unit selected

For hours multiply with 3600000 milliseconds

For Days multiply with 86400000 milliseconds

For weeks multiply with 604800000 milliseconds

For months multiply with 2592000000 milliseconds

If my above script helped please mark it as correct and helpful and close the thread

Regards
Ankur

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

Hi Ankur, 

Definitely I will be marking the thread as completed, but I am getting some issues when I am trying for all 4 options like Hours/Days/Months/Week.

We need to convert the week into Days also as the Duration type field only allows (Days : Hours : Mins : Secs)

For no matter whatsoever I select, its returning me the value in days which is also getting captured in the same fashion on the custom field. Please find the script I am using :

var durationSelected = parseInt(current.variables.estimated_duration);
var gr = new GlideRecord('sc_cat_item');
gr.initialize();
gr.name = current.variables.name;
gr.short_description = current.variables.short_description;
gr.description = current.variables.description;
gr.u_catalog_owner_group = current.variables.group_name.sys_id;
gr.active = 'false';

if (current.variables.unit == 'hours') {
    var hourConversion = parseInt(durationSelected * 60 * 60 * 1000);
    gr.u_est_handle_time.setDateNumericValue(hourConversion);
}
if (current.variables.unit == 'months') {
    var monthConversion = parseInt(durationSelected * 2592000000);
    gr.u_est_handle_time.setDateNumericValue(monthConversion);
}
if (current.variables.unit == 'days') {
    var dayConversion = parseInt(durationSelected * 86400000);
    gr.u_est_handle_time.setDateNumericValue(dayConversion);
} else {
    var weekConversion = parseInt(durationSelected * 604800000);
    gr.u_est_handle_time.setDateNumericValue(weekConversion);
}
gr.insert();

Hi,

Did you use the valid values to compare for hours, days, months etc

try adding logs

var durationSelected = parseInt(current.variables.estimated_duration);

var gr = new GlideRecord('sc_cat_item');
gr.initialize();
gr.name = current.variables.name;
gr.short_description = current.variables.short_description;
gr.description = current.variables.description;
gr.u_catalog_owner_group = current.variables.group_name.sys_id;
gr.active = 'false';

gs.info('unit'+current.variables.unit);

if (current.variables.unit == 'hours') {

    gs.info('inside hours');
    var hourConversion = parseInt(durationSelected * 60 * 60 * 1000);
    gr.u_est_handle_time.setDateNumericValue(hourConversion);
}
if (current.variables.unit == 'months') {

    gs.info('inside months');
    var monthConversion = parseInt(durationSelected * 2592000000);
    gr.u_est_handle_time.setDateNumericValue(monthConversion);
}
if (current.variables.unit == 'days') {

    gs.info('inside days');
    var dayConversion = parseInt(durationSelected * 86400000);
    gr.u_est_handle_time.setDateNumericValue(dayConversion);
} else {

    gs.info('inside weeks');
    var weekConversion = parseInt(durationSelected * 604800000);
    gr.u_est_handle_time.setDateNumericValue(weekConversion);
}
gr.insert();

Regards
Ankur

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