Background script

sparit3737
Tera Contributor

why bacground script is use in services now and whats is important in services now.

5 REPLIES 5

Tarun Rekandar
Giga Guru

Hi @sparit3737,

 

In ServiceNow, Background Scripts are used to execute server-side JavaScript manually and immediately. They are mainly helpful for administrators and developers when they need to test code, troubleshoot issues, or perform one-time operations on records.

 

You can access them from:

 

System Definition → Scripts - Background

 

Common uses of Background Scripts

 

1. Testing server-side scripts

You can quickly test APIs such as "GlideRecord", "gs", or Script Includes without creating a Business Rule.

 

2. Troubleshooting and debugging

Background Scripts can help check record values, identify data issues, and verify whether a server-side query or logic is working correctly.

 

3. One-time bulk updates

They can be used to update multiple existing records when a change is required only once.

 

4. Data cleanup or correction

For example, fixing incorrect values, updating records after an import, or removing unnecessary test data.

 

Example:

var gr = new GlideRecord('incident');

gr.addActiveQuery();

gr.setLimit(10);

gr.query();

 

while (gr.next()) {

    gs.print(gr.number);

}

 

This example retrieves and prints active Incident records.

 

Important considerations

 

Background Scripts run directly on the server and can potentially affect a large number of records. Therefore, it is recommended to:

 

- Test the script in a non-production instance first.

- Verify the query before running any update or delete operation.

- Use a limited number of records when testing.

- Avoid running destructive scripts without proper validation.

 

In simple terms:

 

Background Script = Manual execution + Server-side scripting + Immediate results

 

For ServiceNow development, it is also important to have a good understanding of JavaScript, GlideRecord, Business Rules, Script Includes, Client Scripts, UI Policies, ACLs, Flow Designer, Service Catalog, ITSM, CMDB, and REST integrations.

 

Accept the solution if it assisted you with your question & Mark this response as Helpful.

 

Regards,

Tarun 

Tanushree Maiti
Tera Patron

Hi @sparit3737 

 

Administrators can use the Scripts - Background module to run arbitrary JavaScript code from the server.

The Scripts - Background module consists of the following components.
  • Text box to enter JavaScript.
  • Selector to specify the application scope.
  • Run script button.
  • List of available scopes.
  • Record for rollback? check box. Selected by default. Creates a rollback context for the script execution. After the script is run, click the Script execution and recovery available here link to go to the Script Execution History form where you can rollback the script.
  • Execute in sandbox? check box. Allows script to run with sandbox like restrictions. If checked, data cannot be inserted, updated, or deleted.
  • Execute as scriptlet? check box. Allows the script to execute as a scriplet, which gives the script access to the global scope and any server-side functionality or objects. If the check box is not selected, the script executes in global scope, but does not have access to any server-side functionality or objects.
  • Cancel after 4 hours check box. Selected by default. Cancels the script execution if it continues running after four hours.

When a script is run, the instance displays results, information, and error messages at the top of the screen.

 

Refer: Background scripts 

Using Scripts - Background 

 

Also to to write script ( server side script) efficiently in Background script editor , go through course 

Scripting in ServiceNow Fundamentals (Australia) 

 

 

Aditya_hublikar
Giga Sage

Hello @sparit3737 ,

 

A Background Script in ServiceNow is used to execute server-side scripts manually from the Scripts - Background module. It helps administrators and developers perform operations directly on the database, such as creating, reading, updating, deleting records, updating multiple records, testing GlideRecord queries, and executing custom logic. It is important because it allows us to perform bulk data operations, troubleshoot issues, validate scripts, and automate one-time administrative tasks without creating a separate Business Rule or Scheduled Job. Its major benefits include faster data manipulation, easy script testing, troubleshooting, and reduced manual effort. However, Background Scripts should be used carefully, especially in production instances, because incorrect scripts can modify or delete large amounts of data.

 

If this helps you then mark it as helpful and accept as solution .

regards,

Aditya

yashkamde
Giga Sage

Hello @sparit3737 ,

 

Background scripts are server-side JavaScript execution tools that let administrators run code immediately on the fly without building a formal structure like a Business Rule or Script Include.

 

Why BG is used :

  • Rapid Testing
  • Bulk Data Operations
  • Troubleshooting and Debugging
  • One Time Data Fixes

Importance in ServiceNow :

  • No configuration needed, you simply type the code and click Run script.
  • By using methods like gr.setWorkflow(false) or gr.autoSysFields(false), you can modify data without triggering existing BR, updating the Updated By timestamp.
  • They give full access to the server-side API, global scope, and database operations.

If my response helped mark as helpful and accept the solution.