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.

Print the Incidents which are related to CI

varma2
Mega Sage

Hi All,

 

How to show Incident number which are related to CI Through script.

 

Please suggest,

 

Thanks,

Varma

 

 

 

 

2 REPLIES 2

RAMANA MURTHY G
Mega Sage

Hello @varma2,

Try below background script below, which gives all incident numbers belonging to a particular CI

 

var gr = new GlideRecord('cmdb_ci');
gr.addQuery('name', 'Email');           // Replace Email with your required CI name
gr.query();

if (gr.next()) {
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('cmdb_ci', gr.sys_id);
    grIncident.query();

     while (grIncident.next()) {
            gs.info(grIncident.number);
      }
}

 

 

Find the background script below, which gives all incident numbers belonging to all CIs.

var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
    var grCI = new GlideRecord('cmdb_ci');
    if (grCI.get(gr.cmdb_ci)) {
        gs.print('Incident Number: ' + gr.number + ', CI Name: ' + grCI.name);
    }
}

 

 

Please mark my answer correct & helpful, if it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

AndersBGS
Tera Patron
Tera Patron

Hi @varma2 ,

 

Why through a script? why not just create a report "if that is what is needed" based on the configuration item?

 

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/