- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 06:05 AM
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
Solved! Go to Solution.
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 11:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 09:53 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 11:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 10:09 PM
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.getXML(enddate);
function enddate(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('end_date',answer);
}
var ed = gr.addDays(30);
return ed;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 10:11 PM
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.