- 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-16-2023 09:28 PM
Is your script include client callable?
both the scripts are in same scope?
did you debug by adding gs.info() statements in script include?
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-16-2023 09:56 PM - edited 03-16-2023 09:57 PM
1) Is your script include client callable?
Yes it is
2) both the scripts are in same scope?
Yes
3) did you debug by adding gs.info() statements in script include?
Yes added in the firstDT variable its returning null, where I am passing start date value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 10:06 PM
your parameter name is wrong.
in client script it's sysparm_fdt but in script include you are using sysparm_cdt
So update as this in script include
var firstDT = this.getParameter('sysparm_fdt');
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-16-2023 10:45 PM
Thanks @Ankur Bawiskar , its not returning null value in the firstDT variable
In the log I am printing below variables
firstDT - 17/03/2023 16:42:27
day - 2023-03-17 00:00:00
getting date/time values in different format,
beacuse of this script include returning null value?
- 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