Development

Byte #3: How to Use Background Scripts

Note: This is an updated blog post from 4 years ago on my personal blog. I’ve reviewed it for accuracy…

Note: This is an updated blog post from 4 years ago on my personal blog. I’ve reviewed it for accuracy and ensured it’s Kingston compatible.

I’ll be writing a new post about Background Scripts vs. Fix Scripts soon!

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?

As an admin, use the lock icon to elevate your privileges to “security_admin”.

Dialog box 'Activate an Elevated Privilege' with 'security_admin' checked. OK and Cancel buttons. Look in the left-hand navigation for “ -> Background Scripts”, and click away.UI: Search bar with 'bac', 'Knowledge Base', 'Feedback', 'System Definition', 'Scripts - Background', 'System Logs',…

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.

JavaScript code in a text box, with a 'Run script' button below.

Once you input your script, you’ll click “Run”. The system will run your script, and you’ll see output as pictured below.

Script completed. List of users and their number of project tasks.

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.

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!

In the left hand menu, look for “Upload File”.

Upload File option under System Definition.

Select your JavaScript file, and click upload.

File upload interface: 'Choose File' button, 'Change Task...eminder.js' filename, and 'Upload' button.

Navigate to Background Scripts (remember to elevate to security admin if need be)

UI: Search bar with 'bac', menu with 'Scripts - Background' and 'System Logs' highlighted.

You’ll notice your script listed under the box used to execute JavaScript.

Run script' button above 'customer' with links 'view' and 'run' for 'change task window reminder.js'.

By clicking on the blue link of your script, it will be run instantly, showing your output like normal.

If you ever need to delete one of these scripts, visit the table “v_customer_uploads”, and delete the corresponding record there.

Customer Uploads list with script 'change task window reminder.js' and 'Actions on selected rows...' dropdown.