Here is a script for bulk moving case records from one table to another in HRSD.
var excludedColumns = ['number', 'sys_class_name', 'sys_id', 'sys_mod_count']; //columns that should not be copied
var sourceCOE = 'sn_hr_le_case';
var sourceHRServiceSysId = '1c824aa01b143550fd03a682604bcbb5';
var destinationCOE = 'sn_hr_core_case_workforce_admin';
var destinationHRServiceSysId = '889ece9a8744325077df42e50cbb35bb';
function copyJournal(sourceGR, targetGR) {
//copies the comments and work notes from the sourceGR to the targetGR
var jGR = new GlideRecord('sys_journal_field');
jGR.addQuery('element_id', sourceGR.getUniqueValue());
jGR.query();
while (jGR.next()) {
var newJGR = new GlideRecord('sys_journal_field');
newJGR.initialize();
for (var i in jGR) {
newJGR.setValue(i, jGR.getValue(i));
}
newJGR.setValue('element_id', targetGR.getUniqueValue());
newJGR.setValue('name', targetGR.getRecordClassName());
newJGR.autoSysFields(false);
newJGR.setWorkflow(false);
newJGR.insert();
}
}
function moveAttachments(sourceGR,targetGR) {
//copy the attachments over
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id',sourceGR.getUniqueValue());
attGR.query();
while(attGR.next()){
attGR.setValue('table_sys_id',targetGR.getUniqueValue());
attGR.setValue('table_name',targetGR.getRecordClassName());
attGR.autoSysFields(false);
attGR.setWorkflow(false);
attGR.update();
}
}
function reparentChecklist(sourceGR,targetGR){
var checkGR = new GlideRecord('checklist');
checkGR.addQuery('document',sourceGR.getUniqueValue());
checkGR.setLimit(1);
checkGR.query();
while(checkGR.next()){
checkGR.setValue('table',targetGR.getRecordClassName());
checkGR.setValue('document',targetGR.getUniqueValue());
checkGR.autoSysFields(false);
checkGR.setWorkflow(false);
checkGR.update();
}
}
function reparentHRTasks(sourceGR, targetGR){
//re-parent child tasks
var taskGR = new GlideRecord('sn_hr_core_task');
taskGR.addQuery('parent', sourceGR.getUniqueValue());
taskGR.query();
while (taskGR.next()) {
taskGR.setValue('parent', targetGR.getUniqueValue());
taskGR.setWorkflow(false);
taskGR.autoSysFields(false);
taskGR.update();
}
}
//get the records from the source COE
var sourceGR = new GlideRecord(sourceCOE);
sourceGR.addQuery('hr_service', sourceHRServiceSysId);
//sourceGR.addQuery('sys_id', 'bfcdac3d97f3ee500d56daa00153afeb');
//sourceGR.setLimit(3);
sourceGR.query();
while (sourceGR.next()) {
//if the record already exists, skip it
var existingGR = new GlideRecord(destinationCOE);
existingGR.addQuery('correlation_id', sourceGR.getUniqueValue());
existingGR.query();
if (existingGR.getRowCount() > 0) {
continue;
}
var targetGR = new GlideRecord(destinationCOE);
targetGR.initialize();
//copy all the fields from the source to the target
for (var i in sourceGR) {
if (excludedColumns.indexOf(i) > -1) {
continue;
}
targetGR.setValue(i, sourceGR.getValue(i));
}
//set the HR Service and Correlation fields
targetGR.setValue('hr_service', destinationHRServiceSysId);
targetGR.setValue('correlation_id', sourceGR.getUniqueValue());
targetGR.setValue('correlation_display', sourceGR.number.getDisplayValue());
//set any additional fields that don't have the same name
targetGR.setValue('new_field_name', sourceGR.getValue('old_field_name'));
targetGR.autoSysFields(false);
targetGR.setWorkflow(false);
targetGR.insert();
//update the old record with the new record's sys_id
sourceGR.setValue('correlation_id', targetGR.getUniqueValue());
sourceGR.setValue('correlation_display', newGR.number.getDisplayValue());
sourceGR.autoSysFields(false);
sourceGR.setWorkflow(false);
sourceGR.update();
//update child records
copyJournal(sourceGR,targetGR);
moveAttachments(sourceGR,targetGR);
reparentChecklist(sourceGR,targetGR)
reparentHRTasks(sourceGR,targetGR);
}
