We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Find difference between two dates in minutes

sonita
Giga Guru

I have two fields ( Glide date time) , called outage stat and outage end.

i have another field called Total Outage (in minutes).   This should be auto calculated based off the Outage Start and Outage End fields.

This is what I've done so far ; but doesn't work.

As this a n onchange client script, which field should i select as the change field?( start   I guess?)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

  var strt = g_form.getValue('u_outage_start');

  var end = g_form.getValue('u_outage_end');

  var ajax = new GlideAjax('AjaxDurCalc');

  ajax.addParam('sysparm_name','durCalc');

  ajax.addParam('sysparm_strt',strt);

  ajax.addParam('sysparm_end',end);

  ajax.getXMLWait();

  var answer = ajax.getAnswer();

  g_form.setValue('u_total_outage_in_minutes', answer);

   

}

8 REPLIES 8

Goran WitchDoc
ServiceNow Employee

How does the script include look like? What happens if you put the answer in a alert. what value are you getting back?


this is the script include



var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);


}


});


The alert brings null




I'm not sure which field i should set as the change field


Client script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }


  var strt = g_form.getValue('start_date');


  var end = g_form.getValue('end_date');



  var ajax = new GlideAjax('counttime');



  ajax.addParam('sysparm_name','calcutime');


  ajax.addParam('sysparm_strt',strt);


  ajax.addParam('sysparm_end',end);


  ajax.getXMLWait();


 


  g_form.setValue('u_total_outage_in_minutes', answer);


}



Script include:



var counttime = Class.create();


counttime.prototype = Object.extendsObject(AbstractAjaxProcessor, {




  calcutime: function() {



  var start= this.getParameter('sysparm_strt');


  var end = this.getParameter('sysparm_end');


  result = gs.dateDiff(start, end, true);


  result = result/60;//Reslut in seconds, so divided with 60 to get minutes


  return result;



  },



      type: 'counttime'


});