Calculate duration of two Date/Time fields

Brendan Hallida
Kilo Guru

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.

find_real_file.png

Fields:

TitleNameType
Outage Startu_outage_startDate/Time
Outage Endu_outage_endDate/Time
Total Outage Timeu_total_outage_timeTime

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

9 REPLIES 9

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


clientscript.jpg


Script Include:


Name: AjaxDurCalc


Client callable: Yes


ScriptInclude.jpg



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


_karan
Tera Contributor

Is any one solve this problem???


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.


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


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);  


}


}