How to Use Background Scripts

What?

Background Scripts are Service-Now’s most direct method to run JavaScript directly against the server. This makes them an excellent way to test out the code intended for use in Script Includes, Scheduled Jobs, and Business Rules. Additionally, this is one of the best ways to cobble together text based reports that are too complicated for the reporting module – I’ve often used Background Scripts to aid in outage post-mortem investigations.

Warning!

While powerful, Background Scripts can be dangerous. If you aren’t careful, you can delete all kinds of important or system-critical records. Even more likely, a poorly written script might run indefinitely, causing system performance issues.

Where?

  1. As an admin, use the lock icon to elevate your privileges to “security_admin”.Elevating PrivilegesElevating Privileges Dialogue
  2. Look in the left-hand navigation for “ -> Background Scripts”, and click away.Finding Background Scripts
  3. You’ll see a large box that you can input your JavaScript into, unfortunately there’s no error checking or syntax highlighting, but it’ll have to do.Background Script Box
  4. Once you input your script, you’ll click “Run”. The system will run your script, and you’ll see output as pictured below.Background Script Results
  5. One excellent tip, is that you can use “gs.print()”, to output things from your background script, without having them be entered into the system log – that’s what I’ve done in the image above.

Anything Else?

It is possible to permanently store scripts on the Background Scripts page for use later. In order to do so, you’ll have to write your script in an outside editor, and upload it. These aren’t captured in update sets, so treat them like data.

  1. Write your script and save it with a “.js” file extension – be sure to test it first to make sure it runs as you would expect!
  2. In the left hand menu, look for “Upload File”.Uploading Scripts for Future Use
  3. Select your JavaScript file, and click upload.Upload DialogueSuccessfully Uploaded Script
  4. Navigate to Background Scripts (remember to elevate to security admin if need be).Navigating to Background Scripts
  5. You’ll notice your script listed under the box used to execute JavaScript.Viewing a stored script
  6. By clicking on the blue link of your script, it will be run instantly, showing your output like normal.
  7. If you ever need to delete one of these scripts, visit the table “v_customer_uploads”, and delete the corresponding record there.Deleting an uploaded script

 

Contribute

Have any comments or creative uses for background scripts? Share them in the comments! I’m always looking for new ideas.



Categories: Developers, Scripting Tips

Tags: , , , ,

1 reply

  1. Reblogged this on EcoStratus Technologies @14 Emerton and commented:
    Great tips for #ServiceNow Admin. Careful with background #scripts.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: