- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 07:27 AM
Hello All,
I wrote a before business rule where I need a calculate difference between 2 fields and put the duration value in another field :
field 1 : 'sys_created_on'
field 2 : 'u_resolved_time'
and duration to be copied on 'u_time_to_resolve'
Wrote the below script but it is not working :
var startDate = new GlideDateTime(current.sys_created_on);
var endDate = new GlideDateTime(current.u_resolved_time);
current.u_time_to_resolve = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true);
Can anybody help to understand the miss here ?
Thanks a lot in advance!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 07:53 AM
Hi Swapnil,
try this script once:
var startDate = new GlideDateTime(current.sys_created_on);
var endDate = new GlideDateTime(current.u_resolved_time);
var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); // this would return seconds
current.u_time_to_resolved.setDateNumericValue(diff*1000); // this method accepts milliseconds
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
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
07-29-2019 07:32 AM
Hello Swapnil,
dataDiff returns you difference in seconds. What output you got? What is the type of u_time_to_resolve?
Sample code I have written :
var grTask = new GlideRecord('sc_req_item');
grTask.addQuery('sys_id','5eec52291b3e3300364d32a3cc4bcb08');
grTask.query();
if(grTask.next()){
gs.print('Number'+grTask.number+"|| Created ON: "+grTask.due_date);
var gdt1 = new GlideDateTime(grTask.due_date);
gs.print("CreatedDate: "+gdt1.getDate());
var gdt = new GlideDateTime('2019-07-22 03:56:25');
gs.print("GDT:"+gdt.getDate());
var startDate = gdt1.getDate(); // Replace u_start_date with your field name of Start Date
var currentDate = gdt.getDate();
var diffSeconds = gs.dateDiff(currentDate.getDisplayValue(),startDate.getDisplayValue(), true);
var daysIn = (diffSeconds/(60*60*24));
gs.print(daysIn );
}
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 07:44 AM
Hi Abhishek,
Thanks for your response . Well no value was pushed in the field and type of the field is duration .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 07:53 AM
Hello Swapnil,
Can you please try with my code? also add logs to check where you are issue.
Thanks,
Abhishek
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 07:35 AM
Hi,
You should set your third parameter to false to return a duration data. Please try that once and see if it works
Name | Type | Description |
---|---|---|
startDate | String | The starting date to compare in the current user's date format. |
endDate | String | The ending date to compare in the current user's date format. |
numericValue | Boolean | If true, the return value will be formatted in number of seconds; if false the return value will be formatted ddd hh:mm:ss. |