How to Display Child Incident Count and Numbers in an Incident Form using Arrays
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2025 05:08 AM
I am trying to Display the count of Incident and Incident Numbers on an Incident form with below script. I can get the count right but the Incident numbers are not displaying correctly. I am using an array to store the Incident numbers but when I try to display it shows a random value.
I tried without and without .toString() but error remains.
Error: org.mozilla.javascript.NativeArray@58dccaa9
Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2025 06:07 AM
Hello @abhisnow020
I tried something in background script as per your requirement.
var cont = [];
var gr = new GlideRecord('incident');
gr.addQuery('parent_incident', 'f12ca184735123002728660c4cf6a7ef'); //Since I am running in background script I have passed one sys_id of incident which has multiple childs. You can use current object as you are using your code.
gr.query();
gs.info("Child Incident Count: " + gr.getRowCount()); // since i wanted to print statements i used gs.info you can use addInfoMessage
while(gr.next())
{
cont.push(gr.number.toString());
}
// Join the array elements into a single string
var incidentNumbers = cont.join(", ");
gs.info("Child Incident Numbers: " + incidentNumbers); // since i wanted to print statements i used gs.info you can use addInfoMessage
/*
Output for above code:
Child Incident Count: 3
Child Incident Numbers: INC0010089, INC0010088, INC0007002
*/
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2025 06:15 AM
Please try with below updated code:
(function executeRule(current, previous /*null when async*/) {
var cont = [];
var gr = new GlideRecord('incident');
gr.addQuery('parent_incident', current.sys_id);
gr.query();
gs.addInfoMessage("Total Child Incidents: " + gr.getRowCount()); // Display the count
while (gr.next()) {
cont.push(gr.getValue('number')); // Ensure proper retrieval of incident number
}
if (cont.length > 0) {
gs.addInfoMessage("Child Incidents: " + cont.join(', ')); // Convert array to string
} else {
gs.addInfoMessage("No Child Incidents found.");
}
})(current, previous);
Please mark correct/helpful if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2025 06:18 AM
Hi @abhisnow020
Your code looks good and you can make additional changes as Sunil mentioned if required,
What are the Trigger conditions of your BR?when do you want to show these messages?
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP