Name
sn_hr_core.StandardCaseTransfer
Description
Implements extension point sn_hr_core.HRCaseTransfer Specifies HR Case transfer methods and whether they apply for a specific record.
Script
var StandardCaseTransfer = Class.create();
StandardCaseTransfer.prototype = {
initialize: function() {},
/*
* Returns the sys_id of configuration record used for enablement and visibility
*
* @returns {string} The sys_id of the the corresponding 'sn_hr_core_transfer_case_config' record
*/
getConfigurationId: function() {
return '546587bf3b0033003585802b13efc4fb';
},
/*
* Determines whether this transfer method should be selectable for a specific HR Case record.
* Keep default value (return true) will not add any case-specific filtering.
*
* @param {GlideRecord} grCase - the record of the case to transfer from
*
* @returns {boolean} Whether or not this transfer method should apply for a specific HR Case record.
*/
isCaseEligibleForMethod: function(grCase) {
return true;
},
/*
* Performs the HR Case transfer using this transfer method and provided input data
*
* @param {GlideRecord} grCase - the record of the case to transfer from
* @param {JSON Object} input - Contains properties for specified details required to complete the transfer (e.g. the sys_id of the destination HR Service)
*
* @returns newRecord {GlideRecord} for the newly created record.
*/
transferCase: function(grCase, inputs) {
var selectedService = inputs["selected_service"];
var transferUtils = new sn_hr_core.hr_TransferCase();
var service = new GlideRecord("sn_hr_core_service");
if (!service.get(selectedService)) {
gs.addErrorMessage(gs.getMessage("Could not find selected service"));
return;
}
// Create new case
var newRecord = transferUtils._createCaseFromService(service);
var originalRecordSysId = grCase.getUniqueValue();
// Copy fields from original case
transferUtils._copyFields(grCase, newRecord);
// Copy transfer-specific fields
newRecord.hr_service = selectedService;
newRecord.transferred_from = originalRecordSysId;
if (!newRecord.getValue("universal_request"))
newRecord.comments = gs.getMessage("Case was transferred from {0}", [grCase.number]);
if (service.template.toString())
new sn_hr_core.hr_TemplateUtils().applyBefore(service.template.toString(), newRecord, false);
var newRecordSysId = newRecord.insert();
if (!newRecordSysId) {
gs.addErrorMessage(gs.getMessage("Failed to insert a new case"));
return;
}
var newRecordClass = newRecord.getRecordClassName();
newRecord = new GlideRecord(newRecordClass);
if (!newRecord.get(newRecordSysId)) {
// If the newRecord cannot be queried, try to use setWorkflow(false)
try {
var leTransferUtils = GlidePluginManager.isActive("com.sn_hr_lifecycle_events") ? new sn_hr_le.hr_LETransferUtils() : null;
var erTransferUtils = GlidePluginManager.isActive("com.sn_hr_employee_relations") ? new sn_hr_er.er_TransferUtils() : null;
// Declare a new GlideRecord as isValid returns false if a previous '.get' fails
newRecord = new GlideRecord(newRecordClass);
// If table is in non-sn_hr_core scope, then query as sn_hr_core_case due to security restrictions on setWorkflow API
if ((leTransferUtils && leTransferUtils.isLECase(newRecord))
|| (erTransferUtils && erTransferUtils.isERCase(newRecord)))
newRecord = new GlideRecord('sn_hr_core_case');
newRecord.setWorkflow(false);
newRecord.get(newRecordSysId);
newRecord.setWorkflow(true);
if (!newRecord.isValidRecord())
throw this.type + ': The new case could not be found - ' + newRecordSysId;
} catch(e) {
// Let the transfer continue but give an error message
gs.error('Transfer case exception: ' + e);
gs.addErrorMessage(gs.getMessage('The new case could not be found or you do not have access to view it'));
}
}
if (!newRecord.getValue("universal_request")) {
transferUtils._copyWorkNotes(grCase, newRecord);
transferUtils._copyAttachments(grCase, newRecord);
} else {
transferUtils._copyOnlyWorkNotes(grCase, newRecord);
transferUtils._copyCommentsForUR(grCase, newRecord);
}
// Cancel original case
grCase.state = hr_Constants.CASE_CANCELLED;
grCase.transferred_to = newRecordSysId;
if (!newRecord.getValue("universal_request"))
grCase.comments = gs.getMessage("Case was cancelled and transferred to {0}", [newRecord.number]);
if (!grCase.update()) {
gs.addErrorMessage(gs.getMessage("Failed to close original case"));
return;
}
if (newRecord.isValidRecord())
newRecord.update();
else
gs.addErrorMessage(gs.getMessage('Some fields may not have been transferred over to the new case as you do not have access to the record'));
// Copy interaction_related_record(s)
transferUtils.copyInteractionRelatedRecords(grCase, newRecord);
gs.addInfoMessage(gs.getMessage("Case was transferred from {0}", [grCase.number]));
// Send case transferred email
gs.eventQueue('sn_hr_core.case_transfer', newRecord);
return newRecord;
},
type: 'StandardCaseTransfer'
};
Sys ID
428f23fa3b4c33003585802b13efc484