Convert Time type field value to seconds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 04:32 AM - edited 11-08-2023 04:33 AM
Hello All,
I am using the below Business Rule for getting the time a user works on RITM and CASE.
(function executeRule(current, previous /*null when async*/ ) {
// Check if changes were made to the current RITM or its related Case
if (current.update() || hasCaseUpdates(current)) {
var totalTimeWorked = 0;
var UniqueValue = current.getUniqueValue();
gs.log('Unique value is :' + UniqueValue);
var ParentValue = current.request.parent;
gs.log('Parent value :' + ParentValue);
var TimeEntryRecord = new GlideRecord('task');
TimeEntryRecord.addNotNullQuery('time_worked');
TimeEntryRecord.addQuery('sys_id', 'IN', [UniqueValue, ParentValue]);
TimeEntryRecord.query();
while (TimeEntryRecord.next()) {
var timeWorked = TimeEntryRecord.time_worked.getDisplayValue();
gs.log('Time Worked for ' + TimeEntryRecord.getDisplayValue() + ': ' + seconds + ' seconds');
}
})(current, previous);
The script works perfectly fine and gives the time in time_worked variable. The format of time that I get in this variable is seconds, minutes, hours-minutes, days-hours-minutes which it fetches from the time type field everytime. I want to convert all of these formats into seconds. Is there any method which I can use to convert this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 04:34 AM
I believe it's a duration type field so use this
use this and then convert milliseconds to seconds
var timeWorked = TimeEntryRecord.time_worked.getNumericValue();
timeWorked = timeWorked/1000;
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-08-2023 04:40 AM
@Ankur Bawiskar
I used the above method and it printed an undefined value.
The field type is Timer (Days, hours, minutes, seconds), not Duration (hh:mm:ss)