On change client script to calculate the difference in start date and end date and display the result in another field no_of_days in days

test1231998
Tera Contributor

I have two fields start date and end date for my table.I wanted to write an onchange client script such that upon selection of both dates the difference in dates should be auto populated in no-of_days field which is of type integer.

For eg:If start date is 09-09-2020 and end date is 11-09-2020 The no_of_days field should have 3.

14 REPLIES 14

Yash Agrawal1
Tera Guru

Hello Dipika,

Please check and  Try this

 

Client script:

 

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

 

  var strt = g_form.getValue('<start_field>');//set this as start date

 

  var end = g_form.getValue('<end_field>');//set this as end date

 

  var ajax = new GlideAjax('AjaxDurCalc');

 

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

 

  ajax.addParam('sysparm_strt',strt);

 

  ajax.addParam('sysparm_end',end);

 

  ajax.getXMLWait();

 

  var answer = ajax.getAnswer();

 

  g_form.setValue('<duration_field>', answer);

 

}

 

 

 

Script include

 

Name:AjaxDurCalc

 

Client callable:checked,client:checked

 

script:

 

var AjaxDurCalc = Class.create();

 

AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

calculate: function() {

 

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

 

}

 

});

Please Mark it helpful/correct if my answer helps in any way to resolve your query.

Regards

Yash.K.Agrawal

Hello Deepika,

Did you try the solution i provided, If my solution helped with marked it correct and helpful.which also helps others.

And please keep posted,if wants more help

Regards,

Yash K.Agrawal

helped me. thanks

 

Hi Yash Agrawal1,

This code is worked for Global scope 
when i tried the same script for scoped application, it's not working please help to provide the (scripts ) functions which needs to be used for scoped application.
Thanks in Advance