How to get the difference between 2 date fields in scoped application

vasu17
Tera Contributor

Hi,
Please help on this ,
i want to get difference between 2 date fields (start and end dates) and populate in duration field for the scoped application by using client script 
i have used the script it's worked for global but for the scoped it is not working

here are the scripts both are in same scoped application
Script include:

var DateFormatUtils = Class.create();
DateFormatUtils.prototype = {
    initialize: function() {
    },
calculate: function() {
  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);        
    },
    type: 'DateFormatUtils'
};
client script:
function onChange(control, oldValue, newValue, isLoading) {

  var strt = g_form.getValue('start_date');//set this as start date
  var end = g_form.getValue('end_date');//set this as end date
  var ajax = new GlideAjax('DateFormatUtils');
  ajax.addParam('sysparm_name','calculate');
  ajax.addParam('sysparm_strt',strt);
  ajax.addParam('sysparm_end',end);
 ajax.getXMLAnswer(function(answer){
        if(answer != ''){

  //ajax.getXMLWait();
  //var answer = ajax.getAnswer();
  g_form.setValue('duration', answer);
        }
 });
}
please modify this script if anything wrong for the scoped application
Thanks in advance
8 REPLIES 8

Hello @vasu17 ,

It is working on my side. please do exact same script, it would be work.

Enter proper variable names.

Make sure "Applies on target record" & "Applies on Requested items" is True .

SayaliGurav_0-1697113320148.png

 

Thanks,

Sayali Gurav

 

Ankur Bawiskar
Tera Patron
Tera Patron

@vasu17 

use GlideDateTime subtract method

Your script include won't get called as it's not client callable

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HI @Ankur,

Script include is client callable only, please help on this

vasu17_0-1697035977778.png

Thanks in Advance

Hi @vasu17 

The Class in your Script Include was wrongly generated.

TaiVu_0-1697039220519.png

 

It should be looked like below

 

var DateFormatUtils = Class.create();
DateFormatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    calculate: function() {
        return gs.dateDiff(this.getParameter('sysparm_strt'), this.getParameter('sysparm_end'), false);
    },

    type: 'DateFormatUtils'
});

 

Also make sure you grant the correct user role for this Client Callable Script Include.

 

Cheers,

Tai Vu