Client script to calculate duration

leannen
Kilo Contributor

Hi All

 

I am trying to create a client script which, as soon as a date is entered into a field, it calculates the number of days between that date and today. I have searched the wiki and community and cannot find what I am looking for.

 

Below is what I have so far, but it keeps coming back with an error.   Any help would be much appreciated.

 

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

  var start = nowDateTime();

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

var answer = (start - end);

  g_form.setValue('u_days_since_last_tested', answer);

}

 

Many thanks

Leanne

1 ACCEPTED SOLUTION

I am pasting the code incase the demo gets reset


Client side code:


Script:


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


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


          return;


    }


  var val = g_for m.getValue('u_date_of_last_recovery_test');//compare this with your field


  alert(val);


var ga = new GlideAjax('HelloWorld');


ga.addParam('sysparm_name','helloWorld');


ga.addParam('sysparm_user_name', val);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}


    //Type appropriate comment here, and begin script below


}



>>Script include:HelloWorld (check client callable)


Script:


var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    helloWorld: function() {


      var c = this.getParameter('sysparm_user_name');


      var diff = gs.dateDiff(gs.nowDateTime(), c, true);


          return diff;


    },



    _privateFunction: function() { // this function is not client callable        



    }


});



Please mark answer as correct if it resolves your issue.



Thanks


Pradeep Sharma


View solution in original post

6 REPLIES 6

solutioningnow
Giga Guru

Hi,



Please refer below scripts for your reference:




Script Include:


var DateCalcUtil = Class.create();


DateCalcUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  checkDateDiff: function() {


  var start_date = gs.nowDateTime();


  var end_date = this.getParameter('sysparm_end_date');


  var dif = gs.dateDiff(start_date, end_date);


  return dif;


  }


});



Client Script:


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


  if(!isLoading){


  try{


  var ga = new GlideAjax('DateCalcUtil');


  ga.addParam('sysparm_name','checkDateDiff');


  ga.addParam('sysparm_end_date',newValue);


  ga.getXML(chkDifference);


  }


  catch(e){}


}


}


function chkDifference(response)


{


  var diff=response.responseXML.documentElement.getAttribute("answer");


  alert(diff);


}



Modify the scripts as per requirement.



Please mark answer as correct/helpful, if it was really helpful 🙂



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


leannen
Kilo Contributor

Hi



Thanks for all your assistance, its much appreciated



Regards


Leanne