Add days to a date field/ variable

neha_chauhan
Tera Contributor

I am trying to add 28 days to a date variable based on date selected in another variable (i.e. both variables are mapped with form fields and the client script should run on employee service center and the HR case form as well). I have written an onChange() catalog client script and a global scope script include. But as soon as I fill first date variable error comes : 'There is a JavaScript error in your browser console'. Can anyone guide me on what I am doing wrong. PFB the client script and the script include :

Type: OnChange()

Variable: u_date_concern_reported

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateTimeAmountt');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

 

 

Script Include: ClientDateTimeUtils

Application: Global

Client callable:Checked

Accessible from: All application scopes

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

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype');
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setDisplayValue(firstDT);
day.addDays(addTIME);
return day.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});

1 ACCEPTED SOLUTION

Sufiyan Memon
Kilo Sage
Kilo Sage

Hi neha,

I found some mistakes in your code, I just highlighted that mistake by making those mistakes as Bold. Let me know after correction, are you able to achieve your desire output. If not I can share you some other script as well. 

 

Type: OnChange()

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('global.ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateAmount');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

 

 

Script Include: ClientDateTimeUtils

Application: Global

Client callable:Checked

Accessible from: All application scopes

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

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype');
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDate();
day.setDisplayValue(firstDT);
day.addDays(addTIME);
return day.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});

View solution in original post

10 REPLIES 10

You can set formate by getByFormat("MM/dd/yyyy"). Just update following code at the end of you script include

var day = new GlideDate();
day.setDisplayValue('03-03-2020');
day.addDays(28);
retrun day.getByFormat("MM/dd/yyyy"))

Thank you so much for idiot proofing this for me/the community. 

Worked perfectly the first time.

Who's awesome? You're awesome!

 

- Brett

Thank you for providing this.  This helped a great deal!
I used this script for my purpose. I get the alert with the correct date, however the field I'm setting is not populating with the answer (or anything).

scottangehr_0-1717093968674.pngscottangehr_1-1717094005145.pngscottangehr_2-1717094061424.png

 

function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_sun_date', answer);  //second date field
alert(answer);

 

Here is the format of the field to be populated

scottangehr_3-1717095423938.png

 

Any thoughts as to why?

Thank you in advance

Scott

You can disregard.  My error was there were 2 variables of the same name.
Once I corrected the names - this worked like a charm!!!!

Apeksha Joshi
Kilo Guru

Hi neha ,

try this script :

you can achieve this with the help of catalog client script and script include :

Client script :

var date= g_form.getVlaue('u_date_concern_reported');

var ga = new GlideAjax('getdate');

ga.addParm("sysparm_name","getDetails");

ga.addParam("sysparm_date",date); // VAlue passed to script include

ga.getXML(newdate);

function newdate(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);

g_form.setValue('u_th_day',answer);

}

 
Script Include :
 
var date = this.getParameter('sysparm_date');
 
var gr = GlideDateTime(date);

var ed = gr.addDays(28);

return ed;

Hope this helps !
 
Mark My answer correct and helpful based on Impact .
 
Regards ,
 
Apeksha