- 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
06-28-2022 12:13 AM
The code displays time in seconds, is there a way to display this in days?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 12:30 AM
Hi Swapnil,
Please find below Scripts :
Client Script :
var cdt = g_form.getValue('field_1'); //First Date/Time field
var sdt = g_form.getValue('field_2'); //Second Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will retur
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
Script Include :
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Diff the amount of time between two different Date/Time fields
//params = sysparm_fdt (the first date/time field), sysparm_sdt (second date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(firstDT, secondDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
}
});
Regards,
Sanket