- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 03:41 PM - edited 03-16-2023 09:58 PM
Hello Experts,
Requirement- In the catalog item, 3 variables are there
1) Start time- date/time variable
2) How many days- integer variable
3) End time- date/time variable
I want to do addition of Start time + How many days = End time, and set addition in the end time variable.
What I tried-
I created on change catalog client script
var cdt = g_form.getValue('lease_start'); //Choose the field to add time from
var additionalTime = g_form.getValue('how_many_days_do_you_need_the_software');
var addtime = additionalTime; //The amount of time to add
var addtype = 'day'; //The time type. Can be second, minute, hour, day, week, month, year.
var ajax = new GlideAjax('ITUtils');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
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('lease_end', answer);
alert(answer);
}
}
Script Include
addDateTimeAmount: function() {
var firstDT = this.getParameter('sysparm_cdt'); //If you want to add day/time in current date then no need to pass this parameter
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDateTime(firstDT); // this will give you current date and time
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;
var test = day.getDisplayValue();
return test; // return display value
}
Now script include returing null value,
Can anyone please help me with it,
Thanks,
NS
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 10:56 PM
can you try this
addDateTimeAmount: function() {
var firstDT = this.getParameter('sysparm_cdt'); //If you want to add day/time in current date then no need to pass this parameter
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDateTime(firstDT); // this will give you current date and time
if (addTYPE == 'day') {
day.addDaysUTC(addTIME);
} else if (addTYPE == 'week') {
day.addWeeksUTC(addTIME);
} else if (addTYPE == 'month') {
day.addMonthsUTC(addTIME);
} else if (addTYPE == 'year') {
day.addYearsUTC(addTIME);
} else {
day.addDaysUTC(addTIME);
}
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
var test = day.getDisplayValue();
return test; // return display value
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2023 12:11 AM
Thank you for marking my response as helpful.
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
03-17-2023 12:13 AM
Thanks a lot @Ankur Bawiskar
It worked amezing,
Its always setting 11:00:00 time doesnot matter what is date/time in start date.
e.g.
Start date/time - 18/03/2023 18:06:56
How many days - 2
End date/time - 20/03/2023 11:00:00
Why its setting timealways 11:00:00?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2023 01:10 AM
possibly due to timezone
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader