Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Setting End date field to 30 days based on start date field date selection .

govi1
Giga Contributor

Hi Team,

I have two fields "start date " and "end date," when user selects start date automatically "end date" should be populated with 30 th day of start date.

 

I have written client script and script include , but script is not working I am getting null value in client script alert .Any help would be appreciated.

 

Client script : 

Type : onchange 

Field name : start_date 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//set variable for Valid to field
var cdt = g_form.getValue('start_date');
cdt = cdt.split("-");
cdt = cdt[2] + "-" + cdt[1] + "-" + cdt[0];
cdt = cdt.toString();
var addtime = 30; //The amount of time to remove
var addtype = 'day'; //The time type
//alert(cdt);
var ajax = new GlideAjax('RequestCompletionDate');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(setReviewDate);
function setReviewDate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('end_date', answer);
}
}

 

..........................................

Script include : 

script include name : RequestCompletionDate

function name : addDateTimeAmount

 

var RequestCompletionDate = Class.create();

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

addDateTimeAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDateTime(firstDT);

if(addTYPE == 'second'){day.addSeconds(addTIME);}
else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}
else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}
else if (addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}

//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
day = day.getDisplayValue();
return day;
}

});

 

...................................................................................

Regards,

Govardhan

1 ACCEPTED SOLUTION

Hi,

Since your input field is only date and not date/time, make this 1 line change in your code and it will work.

var selected_date = new Date(getDateFromFormat(newValue, g_user_date_format));

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

 

View solution in original post

8 REPLIES 8

govi1
Giga Contributor

hi noor ,

Thank you for your reply ,

I have tried with your script it works fine when I keep for 5 days ,but if I keep 30 days in the script end date seems to be going back date .please find attached screen shots .

find_real_file.png

 

find_real_file.png

Regards,

Govardhan

Hi,

Since your input field is only date and not date/time, make this 1 line change in your code and it will work.

var selected_date = new Date(getDateFromFormat(newValue, g_user_date_format));

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

 

Megha Padale
Giga Guru

Hi Govardhan ,

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

Client script :

var date= g_form.getVlaue('start_date);

var ga = new GlideAjax('getenddate');

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

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

ga.getXML(enddate);

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

g_form.setValue('end_date',answer);
g_form.setReadonly('end_date',true);

}

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

var ed = gr.addDays(30);

return ed;

 
If my answer is helpful to you, please mark answer as helpful and correct.
 
Thanks and regards,
 
Megha.

Vishwa Prakash
Kilo Expert

Hi, 

I understand that you want to auto populate the 'end date' depending upon the start date. I have done a project earlier with similar requirement. I have attached a code below which I used. Hope this works for you too.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('EndDateSet');
ga.addParam('sysparm_name', 'getEndDate');

ga.addParam('sysparm_expected_end_date_for_this_access', newValue);
ga.getXML(setDate);

function setDate(response)
{
var answer =response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('expected_end_date_for_this_access',answer);
}

Please mark my answer correct and Helpful if you find it of any use.

Thank you.