Name
sn_capi.CAPIFixScriptUtil
Description
No description available
Script
var CAPIFixScriptUtil = 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']
* }
* CAPIFixScriptUtil.deleteRecordsBySysId(tableMap);
*/
CAPIFixScriptUtil.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('CAPIFixScriptUtil: Skipping invalid table: ' + table);
continue;
}
var sysIdChunks = CAPIFixScriptUtil.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('CAPIFixScriptUtil: Deleted ' + curTableDeletedRecords + ' records from table ' + table);
} catch (ex) {
gs.error('CAPIFixScriptUtil: Error while deleting data in table ' + table + ' - Details:' + ex.toString());
}
}
gs.info('CAPIFixScriptUtil: Total records deleted: ' + totalDeletedRecords);
};
CAPIFixScriptUtil.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;
};
CAPIFixScriptUtil.prototype = {
initialize: function() {
},
type: 'CAPIFixScriptUtil'
};
Sys ID
d168c2649f231300e78d317f842e70dc