- 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 07:14 AM
Hi Govardhan,
From your code, it looks like we might add Seconds, Minutes, Hours, Days, Weeks, Months, or Years. First you will need to be sure that you've created a valid GlideDateTime object - in this case it appears you've forgotten your new operator.
Assuming the date string you're passing is already in the system format:
var gdt = new GlideDateTime(firstDT);
if (addTYPE == 'day')
gdt.addDays(addTime);
return gdt;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 09:55 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2020 12:23 AM
The complete solution would look something like this (pay close attention to field names [start_date, duration, end_date] in the client script, as I'm not sure what yours are).
Client Script
Type = onChange
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('RequestCompletionDate');
ga.addParam('sysparm_name', 'getEndDate');
ga.addParam('sysparm_fdt', g_form.getValue('start_date'));
ga.addParam('sysparm_addtime', g_form.getValue('duration'));
ga.getXMLAnswer(setEndDate);
}
function setEndDate(answer) {
g_form.setValue('end_date', answer);
}
Script Include
Client callable = true
var RequestCompletionDate = Class.create();
RequestCompletionDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
var firstDT = this.getParameter('sysparm_fdt');
var addTIME = this.getParameter('sysparm_addtime');
var addTYPE = 'day';
var gdt = new GlideDateTime(firstDT);
if (addTYPE == 'day')
gdt.addDays(addTIME);
return gdt;
},
type: 'RequestCompletionDate'
});
Note: addTYPE is hard-coded for this example.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2020 07:19 AM
Hi Govardhan,
I have written 1 article which is similar to your requirement and also contains sample code. Kindly try this.
Check #4 Add 5 days to the selected Date and set in a new field.
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP