Name
sn_cmp.CMPFixScriptUtil
Description
No description available
Script
var CMPFixScriptUtil = Class.create();
/**
* Deletes records in sn_cmp tables in chunks
* @param tableIdMap - Map of table name to sysId array
* @param (Optional) batchLimit - Batch size for deletion, default is 1000
*
* Sample usage:
* var tableMap = { 'table1': ['sysId1', 'sysId2'],
* 'table2': ['sysId3', 'sysId4']
* }
* CMPFixScriptUtil.deleteRecordsBySysId(tableMap);
*/
CMPFixScriptUtil.deleteRecordsBySysId = function(tableIdMap, batchLimit) {
if (!tableIdMap)
return;
var batchSize = batchLimit || 1000;
var totalDeletedRecords = 0;
for (var table in tableIdMap) {
var curTableDeletedRecords = 0;
if (!new GlideRecord(table).isValid()) {
gs.error('CMPFixScriptUtil: Skipping invalid table: ' + table);
continue;
}
var sysIdChunks = CMPFixScriptUtil.chunkArray(tableIdMap[table], batchSize);
try {
sysIdChunks.forEach(function(chunk) {
var gr = new GlideRecord(table);
gr.initialize();
gr.addQuery('sys_id', 'IN', chunk);
gr.deleteMultiple();
totalDeletedRecords += chunk.length;
curTableDeletedRecords += chunk.length;
});
gs.info('CMPFixScriptUtil: Deleted ' + curTableDeletedRecords + ' records from table ' + table);
} catch (ex) {
gs.error('CMPFixScriptUtil: Error while deleting data in table ' + table + ' - Details:' + ex.toString());
}
}
gs.info('CMPFixScriptUtil: Total records deleted: ' + totalDeletedRecords);
};
CMPFixScriptUtil.chunkArray = function(arr, chunkSize) {
var chunks = [];
if (!Array.isArray(arr))
return null;
// Make a copy of the array so that the original array is not mutated
var arrCopy = [].concat(arr);
while (arrCopy.length)
chunks.push(arrCopy.splice(0, chunkSize));
return chunks;
};
CMPFixScriptUtil.prototype = {
initialize: function() {
},
type: 'CMPFixScriptUtil'
};
Sys ID
03e19fd3db9c2340ab88de01ce9619a5