
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:01 AM
Hello,
On the contract form have 3 fields. Contract end date should be calculated based on Contract start date + duration months. I created a client script and a script include but somehow I always get "null".
I work in scope, but both client script and script include are created in scope.
e.g.
Contract start date: 2022-01-01
Duration months: 12
Contract end date: should be 2022-12-31
My client script:
My script include:
My log from script include returns the correct answer:
My info message from client script returns null:
What am I missing?
Thank you,
Elena
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 09:13 PM
Hi,
Your script include is in Service Module Integration scope so the API name should not start with global.
Can you try creating a fresh script include in your scope and verify this
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:09 AM
Hi,
Hope you are doing good.
Can you share the scripts here and not the screenshots?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:21 AM
Hi,
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gaDurrMonths = new GlideAjax('global.AddendumCalculation');
gaDurrMonths.addParam('sysparm_name', 'endDate');
gaDurrMonths.addParam('sysparm_durrMonths', g_form.getValue('u_duration_months'));
gaDurrMonths.addParam('sysparm_cntrSD', g_form.getValue('starts'));
gaDurrMonths.getXML(updateCntrED);
}
function updateCntrED(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage('answer is: ' + answer);
if(answer){
g_form.setValue("ends", answer);
}
}
Script include:
endDate: function(){
var cntrSD = this.getParameter('sysparm_cntrSD');
var durrMonths = this.getParameter('sysparm_durrMonths');
this.endDateCalculus(cntrSD, durrMonths);
},
endDateCalculus: function(startDate, months){
var gdt = new GlideDateTime(startDate);
var gdtMonth = new GlideDateTime(startDate).getMonthUTC();
var gdtYear = gdt.getYearUTC();
var monthRemainingToAdd = months % 12;
var yearsInMonthsToAdd = (months - monthRemainingToAdd) / 12;
var newMonth = gdtMonth + monthRemainingToAdd;
var finalMonth = newMonth % 12;
if(finalMonth == 0){
finalMonth = newMonth;
var ultimateYearsToAdd = 0;
}else{
var finalyearsInMonthsToAdd = newMonth /12;
ultimateYearsToAdd = Math.floor(finalyearsInMonthsToAdd);
}
var newYear = gdtYear + yearsInMonthsToAdd + ultimateYearsToAdd;
gdt.setYearUTC(newYear);
gdt.setMonthUTC(finalMonth);
gdt.setDayOfMonthUTC(1);
gdt.addDaysUTC(-1);
var endDate = gdt.getValue();
gs.log("endDate in script include is: " + endDate, 'pom0');
return endDate;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:11 AM
Hi,
Check this
1) try to add gs.info() to debug in the script include function; gs.log() will break the code in scoped app
2) Is script include client callable?
3) Try to invoke the Script include with the complete API name at line 6 in client script
4) also use getXMLAnswer() instead of getXML() -> no need to change this if point 1 fixes the issue
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:25 AM
2) Yes, it is client callable
3) + 4) tried that, still getting "null"
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gaDurrMonths = new GlideAjax('global.AddendumCalculation');
gaDurrMonths.addParam('sysparm_name', 'endDate');
gaDurrMonths.addParam('sysparm_durrMonths', g_form.getValue('u_duration_months'));
gaDurrMonths.addParam('sysparm_cntrSD', g_form.getValue('starts'));
gaDurrMonths.getXMLAnswer(updateCntrED);
}
function updateCntrED(answer) {
g_form.addInfoMessage('answer is: ' + answer);
if(answer){
g_form.setValue("ends", answer);
}
}