Diving in the History - Going through old versions of your records (GlideRecordRollback)
There is public API called GlideRecordRollback in ServiceNow that allows you to go through your servicenow record history. Here is a quick script illustrating how to use it.
In this example we will go back to the record history and restore user's first_name that was there before the most recent update.
var user = new GlideRecord('sys_user');
user.get('7e1671c70f43f100aada716ce1050e93'); // Getting a user GlideRecord by sys_id;
var rollBack = new GlideRecordRollback(); // Here is the Force, Luke! :)
var updateCount = user.sys_mod_count; // This is important, we get the update counter.
rollBack.toVersion(user, updateCount-1); // We use the Force (of) NOW! We roll back one version. Now our "user" GlideRecord contains data that was in ServiceNow before the last update was taken.
var oldFirstName = user.first_name; // Got the old First Name (if it was updated there, otherwise you will get the current value)
A few considerations
- As soon as you applied "toVersion" method to your GlideRecord object you would not be able to save it using ".update()'
- This will only work with tables and fields with enabled Audit
- This is a resource consuming operation. Test it with care if you are going to apply it to thousands of records
- You can not go back to desired update number. You have to go one by one, e.g.
rollBack.toVersion(user, updateCount-1); // one update back
rollBack.toVersion(user, updateCount-2); // two updates back
rollBack.toVersion(user, updateCount-3); // three updates back
ServiceNow Senior Architect
9 年Great article. Thank you for sharing.
ServiceNow Apps and Solutions. Value Delivered.
9 年Great stuff! Earlier I had my own class worked similar going at any version back. I did not know there is something oob. Thanks!
Sr ITOM Technical Consultant at ServiceNow
9 年The Force is strong with this one... That's a truly invaluable API!