Using background script i need to fetch the no of incident in all categories for active incidents only.

Deepali Rattan
Tera Contributor

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

1 ACCEPTED SOLUTION

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

View solution in original post

9 REPLIES 9

Dan Ellis
Kilo Sage

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

Prasad Pagar
Mega Sage

Hi @Deepali Rattan 

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

hi @Prasad Pagar 
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();
}
    
   
}

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();
}
    
   
}