
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:02 PM
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 :-
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2020 01:03 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:19 PM
Hi,
I assume you must be having this on record producer
Pick the integer value
Based on unit selected convert the integer value to milliseconds
Then you can set the milliseconds to duration field
refer below link how to set the value
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:41 PM
Hi Ankur,
Thanks for your prompt reply.
Actually, its a catalog item, based on the user value provided, we will be doing the scripting in the Run Script Activity, based on the output from the Run Script Activity, the custom field (type Duration) will be populated on the sc_cat_item table.
I got the 1st part, that based on the units selected we will convert the time into milliseconds, but after that how shall we convert the milliseconds into duration format of Days : Hours : Minutes : Seconds ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2020 12:15 AM
Hi Angshuman,
Please try this script
var avg_time= <value in seconds>
var inputSeconds = parseInt(avg_time, 10);
var secondsInAMinute = 60.0;
var secondsInAnHour = 60.0 * secondsInAMinute;
var secondsInADay = 24.0 * secondsInAnHour;
// extract days
var days = Math.floor(inputSeconds / secondsInADay);
// extract hours
var hourSeconds = inputSeconds % secondsInADay;
var hours = Math.floor(hourSeconds / secondsInAnHour);
// extract minutes
var minuteSeconds = hourSeconds % secondsInAnHour;
var minutes = Math.floor(minuteSeconds / secondsInAMinute);
// extract the remaining seconds
var remainingSeconds = minuteSeconds % secondsInAMinute;
var seconds = Math.ceil(remainingSeconds);
var duration = (hours.toString() + ":" + minutes.toString() + ":" + seconds.toString());
Please mark helpful if it solves the purpose

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2020 12:30 AM
Hi Shivani,
I am getting a bit confused here,
Can provide me the steps like what needs to be done here, 1st do I need to convert the value entered by the user on the variable, then after converting it to milliseconds, you want me to try your script?
If its possible for you, can you please provide me the steps like how shall I achieve the required outcome I have mentioned in the question.
Thanks,
Angshuman