- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2015 04:29 AM
Hi,
In my table I have Start and End fields of "Date/Time" type and "Duration" field of "Integer" type. Based on the duration and start fields, I want End field should get populated. I have written code for this, the value is getting displayed on the form but when I click Submit button the value of End field is not getting saved in database. It is showing empty value.
Here is the code:
Client Script onSubmit:
function onSubmit() {
//Type appropriate comment here, and begin script below
var sdt = g_form.getValue('u_start_date_time'); //Choose the field to add time from
var addtime = g_form.getValue('u_duration_in_minutes'); //The amount of time to add
var addtype = 'minute'; //The time type. Can be second, minute, hour, day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', sdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//You could then take the new Date/Time answer and set the value of another field.
g_form.setValue('u_end_date_time', answer);
g_form.setReadOnly('u_end_date_time', 'true');
alert(answer);
}
}
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a date/time field and adds time to it.
//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - second, minute, hour, day, week, month, year), sysparm_addtime (amount of time to add based on the type).
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;
return day;
}
//type: 'ClientDateTimeUtils'
});
Can anyone help me in resolving this!!!!
Thanks,
Jenny.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2015 09:45 PM
In my client script, I changed the "Type" from onSubmit to onChange and it worked. I was able to store the End Date/Time field value in the database.
Thanks,
Jenny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2015 06:04 AM
You have written a big script and honestly, I haven't looked at it.
Just take the value of duration field which is in milliseconds and convert it to seconds (Multiple by 1000).
Setting the Duration Field Value - ServiceNow Wiki
Now add the resultant seconds value to the start date using addSeconds.
GlideDateTime - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2015 09:55 PM
Hi Kalai,
Thanks for your reply. I got it worked
Thanks,
Jenny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2015 07:10 AM
Can you share the final solution? If the response helped, can you mark it as helpful/correct and close the question?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2015 09:45 PM
In my client script, I changed the "Type" from onSubmit to onChange and it worked. I was able to store the End Date/Time field value in the database.
Thanks,
Jenny