Invalid Function definition Error in logs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 04:07 AM - edited 11-10-2022 07:21 AM
Hi All,
I am using below script in scheduled reports to check the month, but the script is not working
It shows invalid function def in logs
function checkMonths(){
var a = false;
var triggerMonths = [1,3,5,7,9,11];
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
for (i in triggerMonths){
if ( currentMonth == triggerMonths[i])
a=true;
}
return a;
}
checkMonths();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:17 AM
I guess no need to create a function, you can directly check the condition as below:
var triggerMonths = [1,3,5,7,9,11];
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
for (i in triggerMonths){
if ( currentMonth == triggerMonths[i])
return true;
}
return false;
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:24 AM
You cannot have a return statement without function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:17 AM
Try below code :
function checkMonths(){
var triggerMonths = [1,3,5,7,9,11];
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
for (var i=0;i< triggerMonths.length;i++){
if ( currentMonth == triggerMonths[i])
return true;
}
return false;
}
checkMonths();
Please mark answer as Correct or Helpful based on impact.
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:21 AM
Your logic is not efficient one, please use below logic which is way more efficient :
function checkMonths(){
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
if ( currentMonth%2 == 1)
return true;
}
return false;
}
checkMonths();
Please mark answer as Correct or Helpful based on impact.
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 07:23 AM
I have found the solution, function definition is not supported in conditional scripting as it is sandbox script. please use script include to do something like this,