Exploring Advance Reporting Conditions using Script
Introduction :
In ServiceNow, leveraging filter conditions at the list or report level is a seamless process. However, when it comes to implementing intricate or complex conditions that surpass the standard filter capabilities, it's a different ball game. This is where the need for employing scripts, like Script Includes, arises.
Script Includes serve as a robust solution, allowing us to articulate and encapsulate sophisticated logic. By crafting our intricate conditions within a Script Include, we gain the ability to extract the necessary sys_ids. These sys_ids can subsequently be utilized at the filter level, seamlessly integrating our advanced logic into the filtering process.
This approach not only empowers us to overcome the limitations of standard filtering options but also provides a structured and scalable method to manage and deploy complex conditions within ServiceNow's reporting framework.
Problem Statement :
The objective is to generate a report from the incident table that delineates incidents within specific periods. Period 1 spans from January 1st to June 30th, while Period 2 covers July 1st to December 31st. The task involves dynamically determining the current period based on the present date and retrieving incidents falling within that timeframe.
For instance, if today's date were December 10th, 2023, the system would identify this as the 2nd period. Consequently, it should retrieve and display all incidents created between July 1st and December 31st of the current year. This dynamic approach ensures the report reflects incidents relevant to the ongoing period, simplifying the process of monitoring and analyzing incident data within the specified time ranges.
Implementation :
In this approach we are taking help from script include to craft our problem statement logic in that script.
1. Navigate to Script Include under System Definition.
Create Script Include as shown below :
Fig. Script Include Code
Code :
var getCurrentCycle = Class.create();
getCurrentCycle.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCurrentCycleIncident: function(){
var incArr=[];
var currentDate= new GlideDate();
var startDate;
var endDate;
if(currentDate.getMonth()>=1 && currentDate.getMonth()<=6){ //Period 1
startDate=currentDate.getYear()+'-01-01';
endDate=currentDate.getYear()+'-06-30';
}else {
startDate = currentDate.getYear() + '-07-01';
endDate = currentDate.getYear() + '-12-31';
}
var grINC=new GlideRecord('incident');
grINC.addQuery('sys_created_on','>=',startDate);
grINC.addQuery('sys_created_on','<=',endDate);
grINC.query();
while(grINC.next()){
incArr.push(grINC.sys_id.toString());
}
return incArr;
},
type: 'getCurrentCycle'
});
2. Navigate to the report Reports -> Create New
3. Select table as incident
Fig. Report on Incident
3. Apply filter as below in the report :
Fig. Report Filter
Video Implementation & Testing :
Value added in your knowledge ??? Then please mark it as helpful, bookmark it for future use and also share it with your ServiceNow squad.
Regards,Gunjan Kiratkar
Community MVP 2023
Community Rising Star 2022
Youtube : ServiceNow Guy
https://www.servicenow.com/community/developer-articles/exploring-advance-reporting-conditions-using-script/ta-p/2757945