- 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-30-2019 06:56 AM
Hi Ankur,
It worked!! Thanks a lot for your help .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 08:05 AM
Hi swapnil,
Please find the script below, please try this script and make this answer correct if it works for you.
Client Scripting:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
return;
}
var end = g_form.getValue("u_end_date");
var start = g_form.getValue("u_start_date");
var ga = new GlideAjax('yeardate');
ga.addParam('sysparm_name','yeardate');
ga.addParam('sysparm_end',end);
ga.addParam('sysparm_start',start);
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('another_fieldname',answer);
}
Script Include:
var yeardate = Class.create();
yeardate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
yeardate : function()
{
var s = this.getParameter('sysparm_start');
var e = this.getParameter('sysparm_end');
var a = gs.dateDiff(s,e,true);
return a;
},
type: 'yeardate'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 11:30 PM
Swapnil, make sure that the field type is DATE/TIME.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 11:48 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 05:24 AM
it works..!!