---
original_url: "https://jace.pro/blog/how-to-revert-a-bunch-of-records/"
format: markdown
ai_optimized: true
---

How to revert a bunch of records# How to revert a bunch of records

July 9, 2019 [servicenow](/tags/servicenow/)

  Enable AI AnimationToday I was tasked with reverting ~2k stories states to the past value. This is a daunting task as it requires someone to look at the `sys_audit` or `sys_history_line` tables. The code I wrote was a quick fix, but I think others might find it useful.

Without any delay here it is;

`var stories = new GlideRecord('rm_story');
var query = 'closed_by=eb8562c6b52c3000bb05d180e2312616^';
query += 'sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^';
query += 'state=3';
stories.addEncodedQuery(query)
stories.query();
while(stories.next()){ // for each record
  gs.print('in story: ' + stories.getValue('number'));
  var storyID = stories.getValue('sys_id');
  // look up the last audit record for the state field
  var audit = new GlideRecord('sys_audit');
  audit.addQuery('documentkey',storyID);
  audit.addQuery('fieldname','state');
  audit.orderByDesc('record_checkpoint');
  audit.setLimit(1);
  audit.query();
  while(audit.next()){
    gs.print(audit.oldvalue + '=> ' + audit.newvalue);
    stories.setValue('state',audit.oldvalue);
    gs.print('setting state to ' + stories.state.getDisplayValue());
    stories.update();
  }
}`
---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2019/2019-07-09-how-to-revert-a-bunch-of-records.md).

[How to revert a bunch of records](https://jace.pro/blog/how-to-revert-a-bunch-of-records/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/how-to-revert-a-bunch-of-records/*
