Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Condition Script not working (Scheduled Reports)

Artur Schulz
Tera Contributor

Hi based on the incident table i would like to schedule a report if a new specific record entry was made like: Application Service = X AND Priority = P1 OR P2 etc.

 

I use this condition scrip but it is not terminating with true

var count = new GlideAggregate('incident');
count.addEncodedQuery("priorityIN1,2^state=1^opened_atRELATIVEGT@minute@ago@20");
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if(count.next())
   incidents = count.getAggregate('COUNT');
if (incidents >= 1)
    answer = true;
else
    answer = false;
3 REPLIES 3

Kevin Olomu
Tera Contributor
Hello Artur
 
Probably it returns false because there are no new incidents which fulfils the condition? I assume it is required to get incidents which are not older than 20 minutes and have a high priority and are active. If the incident is older than 20 minutes the query will not find it. 
 
I would set the answer variable like this: 
 
var incidentGr = new GlideRecord('incident');
incidentGr.addEncodedQuery("priorityIN1,2^state=1^opened_atRELATIVEGT@minute@ago@20");
incidentGr.query();
answer = incidentGr.hasNext();

 

Best regards
Kevin

Artur Schulz
Tera Contributor

 Hi Kevin,

 

thank you for your support! My goal is to Create a Report based on the incident table with a specific query which shall include a specific application service, priority. Whenever an incident is created based on the query a report auf the incident should be send. I know it is easy to implement with Business rules, but my company restricts access.

 

for the Code i posted first, a table view was working i saw two records an expected the scheduled Report to be triggered. But somehow it did not trigger the report.

 

Thanks

Artur

Amit Gujarathi
Giga Sage
Giga Sage

HI @Artur Schulz ,
I trust you are doing great.
Here's an updated version of your script with explanations:

// Initialize the GlideAggregate for the 'incident' table
var count = new GlideAggregate('incident');

// Add your query here. Ensure that the encoded query is correct and represents your conditions accurately.
// For example, 'application_service=YOUR_SERVICE_ID' should be replaced with the actual service ID.
// 'priorityIN1,2' selects incidents with priority 1 or 2.
// 'state=1' selects incidents in a specific state, adjust as needed.
// 'opened_atRELATIVEGT@minute@ago@20' selects incidents opened in the last 20 minutes.
count.addEncodedQuery("application_service=YOUR_SERVICE_ID^priorityIN1,2^state=1^opened_atRELATIVEGT@minute@ago@20");

// Count the number of incidents matching the query
count.addAggregate('COUNT');
count.query();

// Initialize a variable to hold the count of incidents
var incidents = 0;

// Retrieve the aggregate count if available
if (count.next()) {
    incidents = count.getAggregate('COUNT');
}

// Set 'answer' to true if there is at least one incident, otherwise false
var answer = (incidents >= 1);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi