logo

NJP

MOVE records from one table to another

Import · Nov 18, 2020 · article

Just wanted to save the script I just used to move records form one table to another. We had a custom incident task table before servicenow created one out of box, and wanted to move the records form our custom table to the out of box table.

This script does not convert data or transform to other fields, but if everything is equal between the 2 tables this will MOVE the data and update the numbering

var oldTable = 'u_incident_task'
var newTable = 'incident_task'

var oldTasks = new GlideRecord(oldTable);
oldTasks.query();
oldTasks.autoSysFields(false);
oldTasks.setWorkflow(false);
oldTasks.sys_class_name=newTable;
oldTasks.updateMultiple();

var highestNumber = new GlideRecord(newTable);
highestNumber.setLimit(1);
highestNumber.addQuery('number','!=','');
highestNumber.orderByDesc('number');
highestNumber.query();
highestNumber.next();

var sysNumber = new GlideRecord('sys_number_counter');
sysNumber.get('table',newTable);
sysNumber.number = (highestNumber.number.match(/\d+/g)*1);
sysNumber.update();
View original source

https://www.servicenow.com/community/now-platform-articles/move-records-from-one-table-to-another/ta-p/2318913