The CreatorCon Call for Content is officially open! Get started here.

Calculate Number of Days using Script Include

nehapateria
Kilo Expert

Hello Members,

I am using Demo link (demo016.service-now.com/ ) to create a custom application. It has two Date/Time fields, Start Date and End Date, and one Integer field, Total Number of Days. On the basis of Start Date and End Date I have to calculate the difference between these two dates in No. of Days and have to set the value in the Total Number of Days field.

The issue is :- Total Number of Days field is not showing any value, it is blank, after entering Start Date and End Date

For this I am using Script Include.

Client Script:-

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

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

  var ajax = new GlideAjax('AjaxDurCalc');

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

  ajax.addParam('sysparm_strt',strt);

  ajax.addParam('sysparm_end',newValue);

  ajax.getXML(HelloWorldParse);

function HelloWorldParse(response) {

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

    g_form.setValue('total_number_of_days', val);

}

}

Script Include:-

var AjaxDurCalc = Class.create();

AjaxDurCalc.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

durCalc: function() {

  var sd = new GlideDateTime(this.getParameter('sysparm_strt'));

  var ed = new GlideDateTime(this.getParameter('sysparm_end'));

  var diffSeconds = gs.dateDiff(sd.getDisplayValue(),ed.getDisplayValue(), true);

  var converttodays = diffSeconds/(60*60*24);

  return converttodays;

}

});

Please suggest if there is any correction to be made in the script.

Thanks,

Neha Pateria

5 REPLIES 5

Raju Koyagura
Tera Guru

Code looks good....So better check the variable name and onChange field selected properly or not.



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


is this start or start_date?



Better create date fields instead of date/time, if it is date/time field you may get decimal values (Ex: 1.0008) based on the start date time and end date time. To solve this decimal issue, we have below options.


1. Change date/time to date type


2. Use getDate() method while getting start, end dates


3. Use round method before setting the value in 'total_number_of_days'


Thanks Raju.   It is 'start'.



I tried the below code in Script include but still the field 'total_number_of_days' is blank.



var sd = new GlideDateTime(this.getParameter('sysparm_strt'));


  var ed = new GlideDateTime(this.getParameter('sysparm_end'));


  var sd1 = sd.getDate();


  var ed1 = ed.getDate();


  var diffSeconds = gs.dateDiff(sd1.getDisplayValue(),ed1.getDisplayValue(), true);



If I talk about my original Script include code then return the value in Seconds still the field 'total_number_of_days' doesn't show any value.



  var sd = new GlideDateTime(this.getParameter('sysparm_strt'));


  var ed = new GlideDateTime(this.getParameter('sysparm_end'));


  var diffSeconds = gs.dateDiff(sd.getDisplayValue(),ed.getDisplayValue(), true);


  return diffSeconds;



Is this an issue with the Demo link?     Can you please try the code at your end?



Regards,


Neha Pateria


JJ1
Kilo Guru

I tested your original script and it works.I modified only the field names according to my table.I believe the value return is number of days though decimals are there.


Did you try logging and see at what point it fails to return ? is it calculating the value but not returning anything ?..Just check.



find_real_file.png


Thanks Jeevan!!! Nice to see you in ServiceNow community as well.



So the code is correct. The value should be rounded off to remove the decimals.



Since I am new to ServiceNow so have no idea on how to take logs.



Thanks again.



Neha P.