Name
global.DeepCopy
Description
No description available
Script
var DeepCopy = Class.create();
DeepCopy.prototype = {
addTestManagementCofig: function() {
var pm = new GlidePluginManager();
if (pm.isActive('com.snc.test_management.2.0')) {
var testTablesConfig = {
"tableName": "sn_test_management_m2m_task_test",
"referenceFieldToParent": "task",
"referenceFieldToChild": "test",
"fields": ["test"]
};
this.childTablesConfig.push(testTablesConfig);
}
},
deepCopy: function(oldParentRec) {
var result = {};
var newParentID = this.copySingleRecord(oldParentRec, this.parentFieldsToCopy, this.parentStateVal);
if (gs.nil(newParentID))
return;
for (var i = 0; i < this.childTablesConfig.length; i++)
this.copyRecordsFromTable(this.childTablesConfig[i], newParentID, oldParentRec.getUniqueValue());
result.newStoryLink = oldParentRec.getTableName() + '.do?sys_id=' + newParentID;
result.newStoryID = newParentID;
return result;
},
copyRecordsFromTable: function(childTableConfig, newParentID, oldParentID) {
var gr = new GlideRecord(childTableConfig.tableName);
gr.addQuery(childTableConfig.referenceFieldToParent, oldParentID);
gr.query();
while (gr.next())
this.copySingleRecord(gr, childTableConfig.fields, childTableConfig.stateVal, childTableConfig.referenceFieldToParent, newParentID);
},
copySingleRecord: function(oldRec, fieldsArr, stateVal, referenceFieldToParent, newParentID) {
var newRec = new GlideRecord(oldRec.getTableName());
fieldsArr.forEach(function(field) {
if (!newRec.isValidField(field)) {
gs.warn("Skipping the field {0} as it is not valid on the table {1}", field, newRec.getTableName());
return;
}
if (field === "state")
newRec.setValue(field, stateVal);
else if (field === "short_description")
newRec.setValue(field, 'Copy of ' + oldRec.getValue(field));
else
newRec.setValue(field, oldRec.getValue(field));
}, this);
if (referenceFieldToParent)
newRec.setValue(referenceFieldToParent, newParentID);
return newRec.insert();
},
type: 'DeepCopy'
};
Sys ID
94b9d3005b4f00109dac15233381c738