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

Allen Andreas
Administrator
Administrator

Hello,

I'd recommending to add appropriate logging, such as gs.info() to your script include in various places (beginning, middle, and end) so you can see if the script include is being called at all, then what the values are, then what you're returning to the client.

 

Keep in mind that day.getDisplayValue() for the return in your script include is already string, so there shouldn't be a need to add toString() as well (that could be throwing things off).

 

Additionally, check all other field values and spelling and lastly...ensure that the script include is properly checked as "client callable" and they're in the same scope or use appropriate method of adding the scope to the script include name when calling it via GlideAjax and ensure that it's accessible from that scope, etc.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Mr_X
Mega Guru

Hi,

 

I used to face the same problem. I just converted the values that are received from client side to Script Include to string.
Kindly check with the below code and let me know the update.

 

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(); // 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().toString();

return test; // return display value

}

Ankur Bawiskar
Tera Patron
Tera Patron

@NS16 

can you share the scripts here?

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

Sure @Ankur Bawiskar 

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

}