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.

Reporting how many Incidents are linked to an Change Request

chrifan
Mega Expert

Hi,

I'm want to how many Incidents triggered a Change Request and how many Incidents were triggered by a failed Change. How to build a suitable filter for this purpose? Thx

2 ACCEPTED SOLUTIONS

AndersBGS
Tera Patron
Tera Patron

Hi @chrifan ,

 

For "incidents triggered a change request" you can create a report based on the incident table with the condition:

AndersBGS_0-1700033836210.png

 

For "incidents were triggered by a failed change" you can create a report based on the incident table with the condition:

AndersBGS_1-1700033893037.png

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

best regards

Anders

 

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

View solution in original post

Amit Gujarathi
Giga Sage
Giga Sage

HI @chrifan ,
I trust you are doing great.
Please find the below code 

// Get the current Change Request record
var changeRequest = new GlideRecord('change_request');
if (changeRequest.get(current.change_request)) {
    // Count Incidents linked to this Change Request
    var incidentCount = new GlideAggregate('incident');
    incidentCount.addQuery('related_to', current.change_request);
    incidentCount.addAggregate('COUNT');
    incidentCount.query();

    if (incidentCount.next()) {
        // Display the count on the Change Request form
        current.incidents_linked = incidentCount.getAggregate('COUNT');
    }

    // Count Incidents triggered by this Change Request and marked as Failed
    var failedIncidentCount = new GlideAggregate('incident');
    failedIncidentCount.addQuery('triggered_by', current.change_request);
    failedIncidentCount.addQuery('state', 'Failed'); // Assuming the state field indicates failure
    failedIncidentCount.addAggregate('COUNT');
    failedIncidentCount.query();

    if (failedIncidentCount.next()) {
        // Display the count on the Change Request form
        current.failed_incidents = failedIncidentCount.getAggregate('COUNT');
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

7 REPLIES 7

chrifan
Mega Expert

Thanks to you both,

@AndersBGS this was the direct way I wanted to, indeed. Thank you.

But  the code snippet by @Amit Gujarathi could be helpful for the DevOps too.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @chrifan 

 

Greetings!!

 

As a best practice, avoid script if possible. The solution is already provided by @AndersBGS 

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Dr Atul G- LNG 

Greetings !!

Thanks for the hint.
I regard your code snippet as a backup solution, exit code, last man standing, ...
I appreciate best pratice, I do.

Very best regards
Chris F.