Name
sn_hr_core.CaseTriggerConfigUtil
Description
No description available
Script
var CaseTriggerConfigUtil = Class.create();
CaseTriggerConfigUtil.prototype = {
initialize: function() {
this._errorUtil = new sn_hr_core.ErrorUtil();
this._formatUtil = new sn_hr_core.FormatUtil();
},
createHrCase: function(inputs, outputs) {
var triggerRecordIdForLog = '';
outputs.is_success = 0;
try {
triggerRecordIdForLog = this._getGrIdForLog(inputs.caseValuesGr);
var caseCreateInput = {};
// sample caseFields:
// "opened_for=0a826bf03710200044e0bfc8bcbe5d7a^subject_person=^hr_service=64fa4c53534222003066a5f4a11c0875"
var arrFieldValues = inputs.caseFields.split('^');
for (var idx = 0; idx < arrFieldValues.length; ++idx) {
var arrFieldValue = arrFieldValues[idx].split('=');
caseCreateInput[arrFieldValue[0]] = {
value: arrFieldValue[1]
};
}
gs.info('caseTableName: ' + inputs.caseTableName + ' CaseCreateInput: ' + JSON.stringify(caseCreateInput));
var caseObj = new sn_hr_core.hr_CaseCreation().createTask(inputs.caseTableName, caseCreateInput, true);
if (gs.nil(caseObj))
throw this._errorUtil.getError('Case create error for the trigger table \'' + triggerRecordIdForLog +
'\': Returned caseObj value is null.');
if (!gs.nil(caseObj.errors) && caseObj.errors.length > 0)
throw this._errorUtil.getError(
'Case create error for the trigger table \'' + triggerRecordIdForLog + '\': ' +
JSON.stringify(caseObj.errors));
gs.info('Case created for the trigger table \'{0}\' CaseSysId: {1}',
[triggerRecordIdForLog, caseObj.sys_id]);
outputs.is_success = 1;
outputs.case_sys_id = caseObj.sys_id + '';
} catch (error) {
throw this._errorUtil.handleException('CaseTriggerConfigUtil::createHrCase',
'Problem creating case for the record ' + triggerRecordIdForLog, error);
}
},
checkIfPreviousValuesConditionMatch: function(inputs, outputs) {
var triggerRecordIdForLog = '';
outputs.is_match = 0;
try {
triggerRecordIdForLog = this._getGrIdForLog(inputs.triggerRecordGr);
// if there are no changed fields or the length is zero, we default to not-match the old values.
// We need to have all of the old values condition fields in the changed fields
if (gs.nil(inputs.triggerRecordChangedFields) || inputs.triggerRecordChangedFields.length === 0)
return;
if (!this._checkIfAllOldValuesConditionFieldsExist(inputs))
return;
var previousGr = new GlideRecord(inputs.triggerTableName);
// Iterate over the changed fields array and initialize the previousGr with previous values
for (var idx = 0; idx < inputs.triggerRecordChangedFields.length; ++idx) {
var changedFieldInfo = inputs.triggerRecordChangedFields[idx];
previousGr.setValue(changedFieldInfo.field_name, changedFieldInfo.previous_value);
}
if (GlideFilter.checkRecord(previousGr, inputs.oldValuesCondition))
outputs.is_match = 1;
} catch (error) {
throw this._errorUtil.handleException('CaseTriggerConfigUtil::checkIfPreviousValuesConditionMatch',
'Problem checking previous value match for ' + triggerRecordIdForLog, error);
}
},
_checkIfAllOldValuesConditionFieldsExist: function(inputs) {
var oldValuesConditionFields = new global.HRSecurityUtils().getFieldsFromCondition(inputs.triggerTableName,
inputs.oldValuesCondition);
// changed fields should contain all the fields that exist in oldValuesConditionFields
for (var conditionFieldIdx = 0; conditionFieldIdx < oldValuesConditionFields.length; ++conditionFieldIdx)
if (!this._fieldNameExistsInChangedFields(inputs, oldValuesConditionFields[conditionFieldIdx]))
return false;
return true;
},
_fieldNameExistsInChangedFields: function(inputs, fieldName) {
for (var changedFieldIdx = 0; changedFieldIdx < inputs.triggerRecordChangedFields.length; ++changedFieldIdx) {
var changedFieldInfo = inputs.triggerRecordChangedFields[changedFieldIdx];
if (fieldName == changedFieldInfo.field_name)
return true;
}
// If we cannot find the field name, then we return false;
return false;
},
checkIfCustomTriggerConditionMatch: function(inputs, outputs) {
var triggerRecordIdForLog = '';
outputs.is_condition_met = 0;
try {
triggerRecordIdForLog = this._getGrIdForLog(inputs.triggerRecordGr);
var dateOffsetQuantity = inputs.dateOffsetType === 'after' ? inputs.dateOffsetQuantity :
-1 * inputs.dateOffsetQuantity;
if (gs.nil(inputs.dateOffsetByField))
throw new Error(
this._formatUtil.format('The date offset field \'{0}\' value is empty on the record' + ' \'{1}\'',
inputs.dateOffsetByField, triggerRecordIdForLog));
var offSetTime = new GlideDateTime(inputs.dateOffsetByField);
switch (inputs.dateOffsetUnit) {
case 'days':
offSetTime.addDaysLocalTime(dateOffsetQuantity);
break;
case 'weeks':
offSetTime.addWeeksLocalTime(dateOffsetQuantity);
break;
case 'months':
offSetTime.addMonthsLocalTime(dateOffsetQuantity);
break;
default:
throw new ErrorUtil().getError(
this._formatUtil.format('Trigger Record: \'{0}\', the offset unit \'{1}\' is not handled.',
triggerRecordIdForLog, inputs.dateOffsetUnit));
}
if (new GlideDateTime().after(offSetTime))
outputs.is_condition_met = 1;
} catch (error) {
throw this._errorUtil.handleException('CaseTriggerConfigUtil::checkIfCustomTriggerConditionMatch',
'Problem checking custom case trigger condition for ' + triggerRecordIdForLog, error);
}
},
_getGrIdForLog: function(glideRecord) {
return glideRecord.getTableName() + '::' + glideRecord.getDisplayValue() + '::' +
glideRecord.getValue(sn_hr_core.hr.SYS_ID);
},
type: 'CaseTriggerConfigUtil'
};
Sys ID
40ba8041fb3210101d696a75217069e2