How To Delete (And Restore) Records With A Background Script

Deleting Multiple Records In ServiceNow, a background script is an admins best friend. It allows you to run scripts to directly interact with your database. This can be incredibly helpful and save you a tremendous …

script-execution-and-recovery-options

Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

Deleting Multiple Records

In ServiceNow, a background script is an admins best friend.

It allows you to run scripts to directly interact with your database. This can be incredibly helpful and save you a tremendous amount of time if you have to update hundreds or even thousands of records. There is no need to sit and use the List View to continuously click through each page, sometimes dozens of times. You can simply automate the scripting.

The solution is about 10 lines written on the server. We will show you first how to delete certain records, and then how you can restore them, if needed.

If you have more than a couple hundred records that you either need to delete or update – consider making the changes in a background script.

There are countless reasons why deleting data can be good sometimes. Sometimes, records are incorrectly created or they are decidely not needed anymore. Maybe you’re starting a new application and you’ve decided to not Archive the data. If you are positive that you’d like to start fresh, you can easily remove either some or all of the records on the table with a very simple and quick background script that runs on the server.

Whatever your use case may be, use the below as a guide and modify it to fit your IT Organization. We will throw in a couple of random examples, to show you how you can select certain records and not delete all of the data on a table.

Note: When running a background script, especially when deleting records, be very careful. Data can at times be permentantly deleted or very difficult to restore. Do so with the assistance of an ‘admin’ and always run the script in a sub production environment before running in production. If they data is not audited, then some of the data will not be able to be restored, and relationships between data will also break.

 

How To Delete Multiple Records

how to delete multiple records in servicenow

Role required: admin

Script:

var incident = new GlideRecord(“incident”);
incident.addQuery(“priority”, 1);
incident.setLimit(5);
incident.query();

while (incident.next()) {

gs.log(“Deleting ” + incident.number);

incident.deleteRecord();
}

Here is the background script to delete 5, Priority 1 Incidents in ServiceNow. To delete all records on a table, remove the addQuery() and setLimit() when building out the GlideRecord Query.

addQuery(): I wanted to add this to show that you don’t have to delete all of the records on the table. You can select what records you’d like to delete. So say for example, you only want to delete your P1 Incidents, you’d add the following line to do so:

addQuery(“priority”, 1);

setLimit(): I wanted to add this to show you that you can select how many records the query returns. So say for example you don’t want to delete every single P1, you can just delete 5, or any number that you’d like. This can be really helpful when testing a script in the sub-production instance. You can run it in small batches to see how the system will perform.

setLimit(5);

deleteRecord(): This is the line in the script that brings it all together. Once your GlideRecord query is executed, we have a list of records that the query returns back. When we use deleteRecord() in a while loop, we are iterating through the records and deleting them, once by one. Each time the while loop is reaching a new record, we are executing the deleteRecord() method, which obviously, deletes the record. No parameters are required for this method.

deleteRecord();

How To Restore Data Deleted In A Background Script

ServiceNow, now, has a feature to revert and restore the execution of a background script. This feature has been around for a few Software Family Versions of ServiceNow builds.

Hopefully it never comes to this, but us ServiceNow Admins do sometimes mask mistakes (but never in production!) We are expecting any readers to be first running any background scripts, especially ones that are deleting/updating records, in sub production. So if you have a development or test environment – you should always be doing that there first.

I have not previously experienced this process not working. It is always possible that you write a really complex background script, deleting a tremendous amount of data and supporting data (relationships) and the data is not retrievable. If this ever occurs, we highly suggest you to reach out directly to ServiceNow Support.

When you run a background script and the operation is completed, you are navigated to a results page that looks like the below image.

It provides a full list of the transaction. This includes the scope, time to complete, execution history, system, success and error logs, etc.

script-execution-and-recovery-options

Say you ran this script and it successfully deleted the 5 P1 Incidents that you were hoping for.

But you then realize that you also needed to include another parameter, and you wish you were able to restore that lost data.

Click on the “Script execution history and recovery available here” link.

You are then navigated to the Script Execution History record that is created whenever a background script is run.

background script run - shows execution history

The Script Execution History is automatically generated by the system and is incredibly helpful as a reference.

It’s primarily important because it allows you to revert and rollback the script execution history – fully restoring the records that were deleted.

If you scroll to the bottom of the form, you will see the “Related Links” section and you’ll then see the default UI Action that should be active on the form. This UI Action is called “Rollback Script Execution…”

If you select this button, ServiceNow Pops up a modal on the form asking if you’d like to rollback the background script. Once you type “yes” into the box, the system begins the rollback sequence. With 5 records it takes about a second to restore the data.

script history rollback sequence

At this point, the data has officially been restored. Go and take a look, and if everything was typed in properly – the data will be there.

Have you had any issues running a background script to delete data?

Let us know what you think below.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x