GlideDateTime gives weared result

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2024 08:59 AM
Hello Friends,
I am getting some weird value in GlideDateTime query.
When I am viewing the list view, the duration filed is showing correct value. But while we are pulling the data of that specific field in the reports, we can see a weird result.
Script:
After Insert, update Business rule
(function executeRule(current, previous /*null when async*/ ) {
var inc = new GlideRecord('sysapproval_approver');
inc.addQuery('sys_id', current.sys_id);
inc.query();
if (inc.next()) {
var start = new GlideDateTime(inc.sys_created_on);
var end = new GlideDateTime(inc.u_approved_on);
var dur = GlideDateTime.subtract(start, end); //the difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());
inc.u_duration = dur;
inc.update();
}
}
)(current, previous);
While seeing in the list view, I can see correct value. But when I am trying to pull the data in the report, I am seeing some weired result.
Screenshot is attached.
Duration type field: Duration
Report type: Multipivot
Someone please help to identify the issue.
Regards,
Rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2024 09:41 PM
Hi @Community Alums
Potential Issues in Your Script
1. Assignment of Duration to Field Means.... You calculated the duration by subtracting one GlideDateTime object from another, which gives you a duration. However, assigning this directly to a field (inc.u_duration = dur;) expects the duration to be in a specific format. Without converting dur to a proper duration format (seconds), you might end up setting an unexpected value in the duration field.
2. Duration Format.....>The result from GlideDateTime.subtract() needs to be formatted appropriately before being assigned to a duration field. This formatting requires converting the duration to seconds (or another appropriate unit of measurement that the duration field expects).
var inc = new GlideRecord('sysapproval_approver');
inc.addQuery('sys_id', current.sys_id);
inc.query();
if (inc.next()) {
var start = new GlideDateTime(inc.sys_created_on);
var end = new GlideDateTime(inc.u_approved_on);
var dur = GlideDateTime.subtract(start, end); //the difference between start and end
var durationInSeconds = dur.getNumericValue(); // Get duration in milliseconds
durationInSeconds = durationInSeconds / 1000; // Convert milliseconds to seconds
gs.info(dur.getDisplayValue());
inc.u_duration = durationInSeconds; // Assign duration in seconds
inc.update();
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma