- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 12:36 AM
I have two date time fields "In Time" and "Out Time", the out time would be one hour from the In Time.
I created a on change client script on In Time field. I used the following script, but it was not working.
var timein = g_form.getValue('in_time');
var timeout = g_form.getValue('out_time');
if (timeout === '') {
var timeinDate = new GlideDateTime(timein);
timeinDate.addSeconds(3600);
g_form.setValue('time_out', timeinDate);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 02:31 AM
you want to populate Out time with adding 1 hour to In Time right?
if yes then you can do this with simply onChange client script and no GlideAjax required
var timein = g_form.getValue('in_time');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 1 * 60 * 60 * 1000; // 1 means 1 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('time_out', 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 01:00 AM
Hi AK8977.
Maybe this link can help you, from another question in the community.
But in my opinion you should make some test in your script and put some alerts to show haw value is in the field. Other thing in ServiceNow :
if (timeout === '') --> Use --> if (timeout == '' ")
The strict values not usually works in SN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 01:27 AM
Hi @Ak8977
As @Peter Bodelier suggested you need to write onChange client script and script include to set the date after one hour.
Please follow below script :
Step 1 : Create onChange client script on "in_time" field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (g_form.getValue('in_time')) {
var ga = new GlideAjax("DateTimeUtil"); //script include name
ga.addParam('sysparm_name', "getDate"); //function name
ga.addParam('sysparm_date', g_form.getValue('in_time'));
ga.getXMLAnswer(callBack);
}
function callBack(answer) {
if (answer) {
g_form.setValue('out_time',answer);
}
Step 2 : Write Client Callable Script Include
Name : DateTimeUtil
getDate: function() {
var date = this.getParameter('sysparm_date');
var gdt = new GlideDateTime(date);
gdt.addSeconds(3600);
return gdt;
},
Hope this will help..!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2023 02:31 AM
you want to populate Out time with adding 1 hour to In Time right?
if yes then you can do this with simply onChange client script and no GlideAjax required
var timein = g_form.getValue('in_time');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 1 * 60 * 60 * 1000; // 1 means 1 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('time_out', 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:18 AM
Hello @Ankur Bawiskar ,
thank you very much. The solution worked for me.