Calculate duration of two Date/Time fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 05:42 PM
Hi all,
On the Incident table, I have two Date/Time fields called Outage Start and Outage End. I also have a time field called Total Outage Time.
My requirements are that the Total Outage Time to be automatically calculated from the Outage Start and Outage End fields. If the start or end times change, I would like the total outage time to reflect these changes.
Fields:
Title | Name | Type |
---|---|---|
Outage Start | u_outage_start | Date/Time |
Outage End | u_outage_end | Date/Time |
Total Outage Time | u_total_outage_time | Time |
What is the best way to tackle this? I have seen a few threads and tried a few examples were for onBefore business rules, that did not seem to work, however, this could be me not the code.
Calculating a duration from 2 date / time fields in a scoped application
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
current.u_total_outage_time = GlideDateTime.subtract(new GlideDateTime(current.getValue("u_outage_start")), new GlideDateTime(current.getValue("u_outage_end")));
current.update();
}
Need to calculate difference of date/time fields
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
var outage_start = current.u_outage_start.getDisplayValue();
var outage_end = current.u_outage_end.getDisplayValue();
//If the outage start and end times are present, calculate the duration
if (reported) {
var difference = gs.dateDiff(outage_start, outage_end, false);
current.u_total_outage_time.setValue(difference);
}
}
Thanks in advance,
Brendan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 10:55 PM
Hi Bharath,
My requirement is also similar.Based on difference b/w current time and time entered by user, i want to set duration field.This duration will be then used in Timer activity in workflow to hold timer for that duration.But duration field is setting to Null.No idea , why script is not working.
Note: I am using scoped application .
Client Script code:-
OnChange Client Script
Field Name: u_glide_date_time_1
Script Include:
Name: AjaxDurCalc
Client callable: Yes
But when am testing it, duration field(u_duration_1) is Null .On checking logs, i have found below error.Please help!!
java.lang.SecurityException: AbstractAjaxProcessor undefined, maybe missing global qualifier
Caused by error in Script Include: 'AjaxDurCalc' at line 2
1: var AjaxDurCalc = Class.create();
==> 2: AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3: durCalc: function() {
4: var StartDT = this.getParameter('sysparm_start'); //Start Date-Time Field
5: var EndDT = this.getParameter('sysparm_end'); // End Date-Time Field
Regards
Sheetal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2017 08:09 AM
Is any one solve this problem???

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2017 09:59 AM
Can you please try by adding global in you script include
Object.extendsObject(global.AbstractAjaxProcessor,
OR change the script include scope to Global and try.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 02:01 AM
Hi Brendan,
Just try the same OnChange client script by just adding these few lines( bolded & underlined) to your code so as to get rid of this error:-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var outage_start = g_form.getValue('u_outage_start');
var outage_end = g_form.getValue('u_outage_end');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',outage_start);
ajax.addParam('sysparm_end',outage_end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_outage_time', answer);
}
I believe this will work.
Thanks,
Tarleen Kaur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 02:58 AM
Hi Brendan,
Write an if condition in Onsubmit like:
if((u_outage_start !="") && (u_outage_end != ""))
{
calculateDiff();
}
function:calculateDiff();{
var outage_start = g_form.getValue('u_outage_start');
var outage_end = g_form.getValue('u_outage_end');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',outage_start);
ajax.addParam('sysparm_end',outage_end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_outage_time', answer);
}
}