- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:05 AM
Hi @Ankur Bawiskar @Maik Skoddow @Mohith Devatte @SwarnadeepNandy @Amit Gujarathi please help me on this scenario.
In catalog form based on start date time (variable) , end date time (variable) should be auto populated with 12 hours difference in respective to the selected start date time(variable)
for eg: selected start date/time is 2:26:47 here end date/time variable should auto populate with 12 hours added like 14:26:47 end date/time should be greyed out also.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:30 AM
@Bhanu_thej You need to create client callable script include and onChange client script to achieve this requirement.
Script Include -
var populateEndDate = Class.create();
populateEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setEndDate:function(){
var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
if(startDate){
startDate.addSeconds(43200); //convert 12 hours into seconds and then add
return startDate;
}
},
type: 'populateEndDate'
});
Create onChange client script on change of Start date field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (oldValue != newValue) {
var getEndDate = new GlideAjax('global.populateEndDate');
getEndDate.addParam('sysparm_name', 'setEndDate');
getEndDate.addParam('sysparm_startDate', g_form.getValue('start_date'));
getEndDate.getXMLAnswer(function(answer) {
if (answer != '') {
g_form.setValue('end_date', answer);
}
});
}
}
Replace you start and end date variable name if they are different than mentioned in above script.
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:30 AM
@Bhanu_thej You need to create client callable script include and onChange client script to achieve this requirement.
Script Include -
var populateEndDate = Class.create();
populateEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setEndDate:function(){
var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
if(startDate){
startDate.addSeconds(43200); //convert 12 hours into seconds and then add
return startDate;
}
},
type: 'populateEndDate'
});
Create onChange client script on change of Start date field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (oldValue != newValue) {
var getEndDate = new GlideAjax('global.populateEndDate');
getEndDate.addParam('sysparm_name', 'setEndDate');
getEndDate.addParam('sysparm_startDate', g_form.getValue('start_date'));
getEndDate.getXMLAnswer(function(answer) {
if (answer != '') {
g_form.setValue('end_date', answer);
}
});
}
}
Replace you start and end date variable name if they are different than mentioned in above script.
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:32 AM
just 15-30mins ago I answered similar question and added 1 hour
you can use same script and use onChange on start date/time variable
Sample script
var timein = g_form.getValue('startVariable');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 12 * 60 * 60 * 1000; // 12 means 12 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('endVariable', formatDate(newDT, g_user_date_time_format));
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:33 AM
Hi @Bhanu_thej
Write onchange client Script with Start date.
Scripting Guide - https://servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/
Please mark it Correct and Hit Like if you find this helpful!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 03:35 AM
Hello @Bhanu_thej ,
Use Catalog Client script to achieve it.
// Client script for catalog item form
function onChange(control, oldValue, newValue, isLoading) {
// Get the value of start date time as a GlideDateTime object
var gdt = g_form.getValue('start_date_time');
// Add 12 hours to it
gdt.addSeconds(12 * 60 * 60);
// Set the value of end date time to the modified GlideDateTime object
g_form.setValue('end_date_time', gdt);
}
Hope this works.
Kind Regards,
Swarnadeep Nandy