Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Script Include returning null value

NS16
Mega Sage

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

 

1 ACCEPTED SOLUTION

@NS16 

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

}

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@NS16 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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?

 

@NS16 

possibly due to timezone

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader