ServiceNow – The ‘Background script’ tool.

This tool allow you to run JavaScript executed directly on the instance server.

With the tool you can:

  • Delete single or larger volumes of records from a target table.
  • Add new data to columns in the target table.
  • Modify single or larger volumes of records in a target table.

How to locate the ‘Background script’ tool:

To find the ‘Background script’ tool go to: https://[instance_name].service-now.com/nav_to.do?uri=%2Fsys.scripts.do

Below are some example scripts and how to use them (note – usage of scripts examples below are done at your own risk):

Update Record Example

Update a ‘Location (cmn_location)’ record with values ‘Life Cycle Stage (life_cycle_stage)’ = ‘Operational‘, and ‘Life Cycle Stage Status (life_cycle_stage_status)’ = ‘In Use‘.//Use right click > copy query from SNOW list to get this
var query = “name=BG Script Test“;
//update these with the states to use
var lc_stage = “Operational“;
var lc_status = “In Use“;
try {
//return record count and lock records
var loc = new GlideRecord(“cmn_location“);
loc.addEncodedQuery(query);
loc.query();
gs.info(loc.getRowCount() + ” records found to update”);
while(loc.next()) {
loc.setWorkflow(false); //disable business rules
loc.autoSysFields(false); //disable updated by of who ran script
loc.life_cycle_stage = lc_stage;
loc.life_cycle_stage_status = lc_status;
loc.update();
}
gs.info(“Done”);
} catch(e) {
gs.info (“Error: ” + e);
}
Update Latest Overall Status & Reporting Categorization on Task Table.//Use right click > copy query from SNOW list to get this
var query = “company=a4b84ab26f107e4c8dbaf150bf3ee452^number=INC0402918^ORnumber=INC0402908“;
//update these with the states to use
var lc_reporting = “Value 1“;
var lc_latest = “Value 2“;
try {
//return record count and lock records
var loc = new GlideRecord(“task“);
loc.addEncodedQuery(query);
loc.query();
gs.info(loc.getRowCount() + ” records found to update”);
while(loc.next()) {
loc.setWorkflow(false); //disable business rules
loc.autoSysFields(false); //disable updated by of who ran script
loc.u_reporting_categorization = lc_reporting;
loc.u_latest_overall_status = lc_latest;
loc.update();
}
gs.info(“Done”);
} catch(e) {
gs.info (“Error: ” + e);
}

Delete Records Example

Delete incorrectly created task_sla records. (Note: The query cannot be constructed against against a database view (like for example incident_sla). You need to build the query against a table such as task_sla as data cannot be modified or deleted via a database view).

To navigate to the task.sla table type: task_sla.list

See query format below to be constructed via dot-walking:


https://[instance_name].service-now.com/task_sla_list.do?sysparm_query=task.numberIN&sysparm_view=
/
DELETE SLAS
/
//UPDATE THE QUERY – BE SURE THAT YOU HAVE THE CORRECT QUERY

//QUERY MUST BE SELECTED FROM task_sla not incident_sla!

var query = “task.number=INC0402959“;
try {
var sla = new GlideRecord(“task_sla“);
sla.addEncodedQuery(query);
sla.query();
gs.info(sla.getRowCount() + ” records found to remove”);
sla.deleteMultiple();
gs.info(“Done”);
} catch(e) {
gs.info (“Error: ” + e);
}

Leave a Reply