Background Script Use case.

Akshay Pithore
Tera Contributor

Background Script:

Background Scripts are Server-side scripts that can be immediately run on ServiceNow instance and can be very helpful if you want to try scripts out or fix minor issues on records.

 

1.Write down the script to find out the Count of Active incidents & group them by category.

 

// Create a new GlideAggregate object for the 'incident' table

var count = new GlideAggregate('incident');

 

// Add an active query to include only active records

count.addActiveQuery();

 

// Add an aggregate function to count the number of records

count.addAggregate('COUNT');

 

// Group the results by the 'category' field

count.groupBy('category');

 

// Execute the query

count.query();

 

// Print the total number of rows returned by the query

gs.print(count.getRowCount());

 

// Loop through each grouped result

while (count.next()) {

    // Print the category and the count of incidents in that category

    gs.info(count.getDisplayValue('category') + ' - ' + count.getAggregate('COUNT'));

}

 

 

AkshayPithore_0-1751795315680.png

 

 

2) how to get count of total incidents in system logged

 

var count = new GlideAggregate('incident');

 

 

count.addAggregate('COUNT');

 

 

count.query();   

 

 

if(count.next()) {

 

 

    gs.print(count.getAggregate('COUNT'));

 

 

}

3.Print the state of single incident record without encoded query?

//Find the first active incident record

var gr = new GlideRecord('incident');

if(gr.get('active', true)){

//Do something with the record returned

gs.print(" the number "+ gr.number + ‘-‘+ gr.state);

 

4. Reverse print incident last 10 priority is high.

var c=0;

var check= new GlideRecord('incident');

check.addQuery('priority','1');

check.orderByDesc(‘sys_created_by);

check.setLimit(10);

check.query();

while(check.next())

 

{

c++

gs.print('check the :'+check.number);

}

gs.print('total nnumber of record is:'+c);

 

5.Using Email script send incident to user

 

Assuming that current contains all the incidents:

while(current.next()){

if(current.state =="Code"){

template.print("number :"+current.number);

template.print("Priorty :"+current.priorty);

template.print("CALLER ID :"+current.caller_id);

template.print("short desc:"+${short_description}");

template.print("\n");

 

 


Please mark this response as correct or helpful if it assisted you with your question.
3 REPLIES 3

GlideFather
Tera Patron

Hi, @Akshay Pithore ,

 

Nice overview about the background script, let me add few important details to it:

 

KamilTEL_1-1751798495060.png

 


what is also very helpful when it comes to background script is the Script Execution History:

 

  • it still stores the script, so if you executed one yesterday then forgot to save it and now you need it, this is where you can find it 

the Rollback Contexts:

  • in case that your script made some damage and you want to revert it, the Rollback is the way for it,
    • NOTE: be always sure that this option is marked true:

KamilTEL_2-1751798619331.png

 

When you are updating records in PROD, always use the setLimit(); option, I had a typeo which ended that all the records were evaluated to be updated, completely ignoring my condition 😄 so when I wanted to update 400 records I updated 32K record 😄 😄 😄

 

If the very same script is going to be executed often, consider saving it as a FIX SCRIPT.

 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


@GlideFather 

Thanks for sharing.


Please mark this response as correct or helpful if it assisted you with your question.

GlideFather
Tera Patron

And for those who use SNUtils browser extension, if you want to do quick check or data fix, this is the way:

 

  • go to the table where is what you want to query
    • e.g. sys_user
  • click the extension button, select GlideRecord
    • a draft is auto-created,
    • only to print the values - go to edit up to your need
  • click open in backgroundscript:

KamilTEL_0-1751799067040.png

 

More details about SNUtils > check the Arnoud Kooi on LinkedIn or their website

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */