Name
sn_hr_core.hr_BulkCaseUpload
Description
No description available
Script
function BatchLookup(options) {
this.batchSize = options.batchSize;
this._values = [];
this._tracking = {};
this._stats = {
totalCount: options.totalCount,
entered: 0,
processed: 0
};
}
BatchLookup.prototype = {
appendValue: function(value) {
if (this.isBatchFull()) return;
this._stats.entered++;
var tracking = this._tracking[value] || {
count: 0,
firstIndex: this._values.length
};
this.trackValue(value, {
count: tracking.count + 1,
firstIndex: tracking.firstIndex
});
// duplicates are admissible, firstIndex marks the first which enters
this._values.push(value);
},
drainBatch: function() {
var self = this;
this._stats.processed += this._values.length;
var lookupResults = this._values.map(function(value) {
return {
value: value,
tracking: self._mergeObject({}, self._tracking[value])
};
});
this._values = [];
return lookupResults;
},
isBatchFull: function() {
return this._values.length === this.batchSize;
},
getBatchValues: function() {
return this._values;
},
getProgress: function() {
return Math.floor(this._stats.entered / this._stats.totalCount * 100);
},
trackValue: function(value, info) {
this._tracking[value] || (this._tracking[value] = {});
this._mergeObject(this._tracking[value], info);
},
aggregateTrackingLogs: function(mapFn, reduceFn) {
// map-reduce tracking values
var self = this;
var accumulator = reduceFn ? {} : [];
mapFn || (mapFn = function(trackRecord) {
return trackRecord;
});
reduceFn || (reduceFn = function(acc, trackRecord) {
acc.push(trackRecord);
});
return Object.keys(this._tracking).reduce(function(acc, trackRecordValue) {
var mappedRecord = mapFn(self._tracking[trackRecordValue]);
if (mappedRecord) {
reduceFn(acc, mappedRecord);
}
return acc;
}, accumulator);
},
_mergeObject: function() {
var args = Array.prototype.slice.call(arguments);
var objAccumulator = args.shift();
var objOverride;
while (args.length) {
objOverride = args.shift() || {};
Object.keys(objOverride).forEach(function(key) {
objAccumulator[key] = objOverride[key];
});
}
return objAccumulator;
}
};
var hr_BulkCaseUpload = Class.create();
hr_BulkCaseUpload.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
type: 'hr_BulkCaseUpload',
uploadOperation: function(firstHeader, searchList) {
var tracker = GlideExecutionTracker.getLastRunning();
tracker.run();
if (searchList.length != 0)
searchList = searchList.split(",");
else
searchList = [];
var bulkCaseRunGR = new GlideRecord("sn_hr_core_bulk_case_creation_run");
var bulkCaseDataGR = new GlideRecord("sn_hr_core_bulk_case_creation_data");
var userFound = 0;
var userNotFound = 0;
var userDuplicates = 0;
var foundList = [];
// create a new record for bulk case run table
bulkCaseRunGR.initialize();
var bulkCaseRunSysID = bulkCaseRunGR.sys_id;
var bulkCaseRunNumber = bulkCaseRunGR.number;
bulkCaseRunGR.insert();
var timeStartStr = this._currentDateGenerateTimeStr();
//search and import uploaded data to bulk case data table
var currentList = [];
var totalRecords = searchList.length;
var lookupConfig = null;
if (firstHeader === "email") {
lookupConfig = {
bulkCaseRunField: "user_name",
userFilterField: "email"
};
} else if (firstHeader === "user_name") {
lookupConfig = {
bulkCaseRunField: "email",
userFilterField: "user_name"
};
}
if (lookupConfig) {
var batchLookup = new BatchLookup({
totalCount: totalRecords,
batchSize: 200
});
var currentProgress, lastProgress = 0;
var sysUserGR;
for (i = 0; i < totalRecords; i++) {
currentProgress = batchLookup.getProgress();
if (currentProgress > lastProgress) {
tracker.incrementPercentComplete(currentProgress - lastProgress);
lastProgress = currentProgress;
}
batchLookup.appendValue(searchList[i].toLowerCase());
if (!batchLookup.isBatchFull() && i < totalRecords - 1) {
continue;
}
sysUserGR = new GlideRecord("sys_user");
sysUserGR.addQuery(
lookupConfig.userFilterField,
"IN",
batchLookup.getBatchValues().join(",")
);
sysUserGR.query();
while (sysUserGR.next()) {
// store log of retrieved user records
batchLookup.trackValue(sysUserGR.getValue(lookupConfig.userFilterField).toLowerCase(), {
sys_id: sysUserGR.getValue('sys_id'),
email: sysUserGR.getValue('email'),
user_name: sysUserGR.getValue('user_name'),
});
}
// empty the batch while conserving the tracking logs
batchLookup.drainBatch().forEach(function(userLookup, idx) {
var result = [userLookup.tracking.sys_id ? "Found" : "Not Found"];
if (userLookup.tracking.count > 1 && idx > userLookup.tracking.firstIndex) {
result.push("Duplicate");
userDuplicates++;
}
bulkCaseDataGR.initialize();
bulkCaseDataGR.bulk_case_creation_run = bulkCaseRunSysID;
bulkCaseDataGR.query_result = result.join(" & ");
bulkCaseDataGR.user = userLookup.tracking.sys_id;
bulkCaseDataGR.user_name = userLookup.tracking.user_name;
bulkCaseDataGR.email = userLookup.tracking.email;
if (!userLookup.tracking.sys_id) {
// log the input value if user not found
bulkCaseDataGR[lookupConfig.userFilterField] = userLookup.value;
}
bulkCaseDataGR.insert();
});
}
foundList = batchLookup.aggregateTrackingLogs(function(trackRecord) {
return trackRecord.sys_id;
});
userFound = foundList.length;
userNotFound = totalRecords - userFound - userDuplicates;
}
var timeEndStr = this._currentDateGenerateTimeStr();
bulkCaseRunGR.addQuery("sys_id", bulkCaseRunSysID);
bulkCaseRunGR.query();
if (bulkCaseRunGR.next()) {
bulkCaseRunGR.total_records = totalRecords;
bulkCaseRunGR.user_found = userFound;
bulkCaseRunGR.user_not_found = userNotFound;
}
bulkCaseRunGR.update();
var url = "/sn_hr_core_bulk_case_creation_data" + "_list?sysparm_query=sys_created_on>=" + timeStartStr + "^sys_created_by=" + gs.getUserName() + "^sys_created_on<=" + timeEndStr + "&sysparm_domain_restore=false&sysparm_stack=no";
var encodedQuery = "";
for (var i = 0; i < foundList.length; i++) {
if (i == foundList.length - 1)
encodedQuery += "sys_id=" + foundList[i] + "^EQ";
else
encodedQuery += "sys_id=" + foundList[i] + "^OR";
}
tracker.updateResult({
recordCount: totalRecords,
bulkCaseRunID: bulkCaseRunSysID,
url: url,
userFound: userFound,
userNotFound: userNotFound,
encodedQuery: encodedQuery
});
},
//Used in encoded query generation for time comparison
_currentDateGenerateTimeStr: function() {
var currentTime = new GlideDateTime();
currentTime = currentTime.getDisplayValueInternal().toString();
currentTime = currentTime.split(" ");
var date = currentTime[0];
var time = currentTime[1];
return "javascript:gs.dateGenerate('" + date + "','" + time + "')";
}
});
Sys ID
5776131c0bf032001c252c7885673aa6