- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 11:00 AM
Hi
Using background script i need to fetch the no of incident in all categories for active incidents only.
Can someone help me here?
I am able to fetch the no of incident records.
How can i fetch the no of record for each category in BACKGROUND Script ONLY.
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 12:12 PM
Hi,
Try this
var incidents = new GlideRecord('incident');
incidents.addEncodedQuery('active=true^subcategory!=');
incidents.query();
while (incidents.next()) {
var category = incidents.category;
var subcategory = incidents.subcategory;
gs.print('cat' +category);
gs.print('sub' +subcategory);
if(category=='inquiry' && subcategory=='antivirus')
{
gs.print('sssss');
incidents.subcategory='software';
incidents.subcategory='email';
incidents.setWorkflow(false);
incidents.update();
}
}
Thank you
Prasad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 11:13 AM
I've tested this code on my dev instance and it works.
var incidents = new GlideAggregate('incident');
incidents.addQuery('active', true); // Only include active incidents
incidents.addAggregate('count', 'category'); // Add an aggregate to count based on category field
incidents.orderBy('category');
incidents.query();
while (incidents.next()) {
// Get the current category and count
var category = incidents.category;
var count = incidents.getAggregate('count', 'category');
gs.info('Category: ' + category + ' Count: ' + count);
}
Hope this helps!
Thanks,
Dan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 11:18 AM
Hi
Try this
var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'category');
agg.orderBy('category');
agg.query();
while (agg.next()) {
//do things on the results
var incidentCount = agg.getAggregate('COUNT', 'category');
var category = agg.getValue('category');
gs.info('Display the count {0} and category {1}', [incidentCount, category]);
}
Thank you
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 11:40 AM
hi
I need to update the categories and subcategroies using background script:
Here is the code am using:
Can you help me what am i doing wrong?
var incidents = new GlideAggregate('incident');
incidents.addEncodedQuery('active=true^subcategory!=NULL');
incidents.addAggregate('count', 'category');
incidents.addAggregate('scount', 'subcategory');
incidents.orderBy('category');
incidents.orderBy('subcategory');
incidents.query();
while (incidents.next()) {
//gs.print(incidents.getRowCount())
var category = incidents.category;
var count = incidents.getAggregate('count', 'category');
var subcategory = incidents.subcategory;
var scount = incidents.getAggregate('count', 'subcategory');
gs.print(count+" "+category);
gs.print(scount+" "+subcategory);
if(category=='inquiry' && subcategory=='antivirus')
{
incidents.category='software';
incidents.subcategory='email';
incidents.update();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2021 11:51 AM
Hi Try this
var incidents = new GlideAggregate('incident');
incidents.addEncodedQuery('active=true^subcategory!=NULL');
incidents.addAggregate('count', 'category');
incidents.addAggregate('count', 'subcategory');
incidents.orderBy('category');
incidents.orderBy('subcategory');
incidents.query();
while (incidents.next()) {
//gs.print(incidents.getRowCount())
var category = incidents.category;
var count = incidents.getAggregate('count', 'category');
var subcategory = incidents.subcategory;
var scount = incidents.getAggregate('count', 'subcategory');
gs.print(count+" "+category);
gs.print(scount+" "+subcategory);
if(category=='inquiry' && subcategory=='antivirus')
{
incidents.category='software';
incidents.subcategory='email';
incidents.update();
}
}