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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@NS16 

Is your script include client callable?

both the scripts are in same scope?

did you debug by adding gs.info() statements in script include?

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

Hi @Ankur Bawiskar 

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

@NS16 

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');

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

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?

@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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader