Name
sn_risk_advanced.RiskEventApprovalRulesBase
Description
No description available
Script
var RiskEventApprovalRulesBase = Class.create();
RiskEventApprovalRulesBase.prototype = {
initialize: function() {},
getResponseTemplateForEvent: function(riskEventRecord) {
return this._getResponseTemplateForEvent(riskEventRecord);
},
assignOwnerToEventFromTemplate: function(riskEventRecord) {
return this._assignOwnerToEventFromTemplate(riskEventRecord);
},
getThresholdValue: function(responseTemplate, riskEvent) {
return this._getThresholdValue(responseTemplate, riskEvent);
},
shouldIssueBeAutoCreated: function(responseTemplate, thresholdValue, riskEvent) {
return this._shouldIssueBeAutoCreated(responseTemplate, thresholdValue, riskEvent);
},
shouldTaskBeAutoCreated: function(responseTemplate, thresholdValue, riskEvent) {
return this._shouldTaskBeAutoCreated(responseTemplate, thresholdValue, riskEvent);
},
getThresholdRecords: function(responseTemplate, riskEvent, thresholdValue) {
return this._getThresholdRecords(responseTemplate, riskEvent, thresholdValue);
},
isResponseTemplateAlreadyExists: function(riskEventResponseTemplate) {
return this._isResponseTemplateAlreadyExists(riskEventResponseTemplate);
},
isNonFinancialRuleAlreadyExists: function(nonFinancialRuleRecord) {
return this._isNonFinancialRuleAlreadyExists(nonFinancialRuleRecord);
},
isFinancialRuleAlreadyExists: function(financialRuleRecord) {
return this._isFinancialRuleAlreadyExists(financialRuleRecord);
},
canCloseEvent: function(responseTemplate, riskEventId, createIssue) {
return this._canCloseEvent(responseTemplate, riskEventId, createIssue);
},
closeEvent: function(eventId) {
return this._closeEvent(eventId);
},
isValidThresholdRule: function(thresholdRule, table, column) {
return this._isValidThresholdRule(thresholdRule, table, column);
},
canShowApproveButton: function(riskEvent) {
return this._canShowApproveButton(riskEvent);
},
_isValidThresholdRule: function(thresholdRule) {
var table = thresholdRule.getTableName();
var gr = new GlideAggregate(table);
gr.addAggregate('COUNT');
gr.addQuery('response_template', thresholdRule.response_template);
gr.addQuery('sys_id', '!=', thresholdRule.getUniqueValue());
var query = null;
var message = "";
var thresholdValue = "";
var column = "";
if (table == "sn_risk_advanced_financial_impact_approval_threshold") {
column = "amount";
thresholdValue = thresholdRule.amount.getReferenceValue();
} else {
column = "impact";
thresholdValue = thresholdRule.getValue(column);
}
if (thresholdRule.is_approver_required == 0) {
if (table == "sn_risk_advanced_financial_impact_approval_threshold") {
message = gs.getMessage("This threshold cannot be created because either a threshold with no approvers already exists or the current amount is greater than the existing amount that needs an approver.");
} else {
message = gs.getMessage("This threshold cannot be created because either a threshold with no approvers already exists or the current impact is greater than the existing impact that needs an approver.");
}
query = gr.addQuery(column, '<', thresholdValue);
query.addOrCondition('is_approver_required', '0');
} else {
if (table == "sn_risk_advanced_financial_impact_approval_threshold") {
message = gs.getMessage("This threshold cannot be created because a threshold with amount greater than current amount have no approvers.");
} else {
message = gs.getMessage("This threshold cannot be created because a threshold with impact greater than current impact have no approvers.");
}
query = gr.addQuery(column, '>', thresholdValue);
query.addCondition('is_approver_required', '0');
}
gr.query();
var count = 0;
if (gr.next()) {
count = gr.getAggregate('COUNT');
}
var result = {};
result.valid = count == 0;
result.message = message;
return result;
},
_assignOwnerToEventFromTemplate: function(riskEventRecord) {
var result = this._getResponseTemplateForEvent(riskEventRecord);
var responseTemplate = result.responseTemplate;
if (responseTemplate) {
if (responseTemplate.getValue("event_owner_assignment_type") == "1" && riskEventRecord.assigned_to.nil()) {
riskEventRecord.setValue('assigned_to', responseTemplate.getValue('event_owner'));
} else if (responseTemplate.getValue("event_owner_assignment_type") == "2" && riskEventRecord.assignment_group.nil()) {
riskEventRecord.setValue('assignment_group', responseTemplate.getValue('event_owning_group'));
}
}
},
_closeEvent: function(eventId) {
var result = this._getResponseTemplateForEvent(eventId);
var responseTemplate = result.responseTemplate;
if (responseTemplate && this._canCloseEvent(responseTemplate, eventId, false)) {
this._updateRiskEventState(eventId, "3", 'This is a system generated message: This risk event was configured to be closed when all the underlying issues and tasks get closed. If you do not want an event to automatically close, contact your risk administrator.');
}
},
_updateRiskEventState: function(eventId, state, comment) {
var riskEvent = new GlideRecord('sn_risk_advanced_event');
riskEvent.get(eventId);
riskEvent.setValue('state', state);
if (comment) {
riskEvent.comments = comment;
}
riskEvent.update();
},
_canCloseEvent: function(responseTemplate, riskEventId, creatingIssueOrTasks) {
return responseTemplate.getValue('auto_close_event_once_approved') == 1 && new sn_risk_advanced.GRCRiskEvent().canEventClose(riskEventId) && !creatingIssueOrTasks;
},
_isFinancialRuleAlreadyExists: function(financialRuleRecord) {
var gr = new GlideRecord('sn_risk_advanced_financial_impact_approval_threshold');
gr.addQuery('amount', financialRuleRecord.amount.getReferenceValue());
gr.addQuery('response_template', financialRuleRecord.response_template);
gr.addQuery('sys_id', '!=', financialRuleRecord.getUniqueValue());
gr.query();
return gr.next();
},
_isNonFinancialRuleAlreadyExists: function(nonFinancialRuleRecord) {
var gr = new GlideRecord('sn_risk_advanced_non_financial_impact_approval_threshold');
gr.addQuery('impact', nonFinancialRuleRecord.impact);
gr.addQuery('response_template', nonFinancialRuleRecord.response_template);
gr.addQuery('sys_id', '!=', nonFinancialRuleRecord.getUniqueValue());
gr.query();
return gr.next();
},
_isResponseTemplateAlreadyExists: function(riskEventResponseTemplate) {
var gr = new GlideRecord('sn_risk_advanced_risk_event_response_template');
gr.addQuery('entity', riskEventResponseTemplate.entity);
gr.addQuery('event_type', riskEventResponseTemplate.event_type);
gr.addQuery('sub_type', riskEventResponseTemplate.sub_type);
gr.addQuery('category', riskEventResponseTemplate.category);
gr.addQuery('sys_id', '!=', riskEventResponseTemplate.getUniqueValue());
gr.query();
return gr.next();
},
_getThresholdRecords: function(responseTemplate, riskEvent, thresholdValue) {
if (responseTemplate.event_type == 1) {
return this._getFinancialThresholdRecords(responseTemplate, riskEvent, thresholdValue);
} else {
return this._getNonFinancialThresholdRecords(responseTemplate, riskEvent, thresholdValue);
}
},
_getResponseTemplateForEvent: function(riskEventRecord) {
var upstreamIDs = [];
var ruleResult = {};
if (riskEventRecord.primary_profile.active)
this._getFirstApplicableResponseTemplate([riskEventRecord.primary_profile], upstreamIDs, ruleResult, riskEventRecord);
return ruleResult;
},
_getFirstApplicableResponseTemplate: function(currentLevelProfiles, result, ruleResult, riskEventRecord) {
for (var i = 0; i < currentLevelProfiles.length; i++) {
if (this._checkIfEntityHasRule(currentLevelProfiles[i], riskEventRecord, ruleResult)) {
return;
}
}
var relationships = new GlideRecord('sn_grc_m2m_profile_profile');
relationships.addQuery('downstream_profile', "IN", currentLevelProfiles);
relationships.orderBy('sys_created_on');
relationships.query();
var nextLevelProfiles = [];
if (!relationships.hasNext())
return;
while (relationships.next()) {
var profile = relationships.getValue('upstream_profile');
if (relationships['upstream_profile'].active && result.indexOf(profile) == -1) {
nextLevelProfiles.push(profile);
result.push(profile);
}
}
this._getFirstApplicableResponseTemplate(nextLevelProfiles, result, ruleResult, riskEventRecord);
},
_checkIfEntityHasRule: function(profileId, riskEventRecord, ruleResult) {
var gr = new GlideRecord('sn_risk_advanced_risk_event_response_template');
gr.addQuery('entity', profileId);
gr.addQuery('event_type', riskEventRecord.event_type);
gr.addQuery('sub_type', riskEventRecord.sub_type);
var q2 = gr.addQuery('category', riskEventRecord.category);
q2.addOrCondition('category', '100');
gr.query();
if (gr.next()) {
if (gr.getRowCount() == 2 && gr.getValue('category') == '100') {
gr.next();
}
ruleResult.responseTemplate = gr;
return true;
}
return false;
},
_getNonFinancialThresholdRecords: function(responseTemplate, riskEvent, thresholdValue) {
var gr1 = new GlideRecord('sn_risk_advanced_non_financial_impact_approval_threshold');
gr1.addQuery('response_template', responseTemplate.getUniqueValue());
gr1.addQuery('impact', '<=', thresholdValue);
gr1.addQuery('is_approver_required', '1');
gr1.orderBy('impact');
gr1.query();
return gr1;
},
_getFinancialThresholdRecords: function(responseTemplate, riskEvent, thresholdValue) {
var gr = new GlideRecord('sn_risk_advanced_financial_impact_approval_threshold');
gr.addQuery('response_template', responseTemplate.getUniqueValue());
gr.addQuery('amount', '>=', thresholdValue);
gr.orderBy('amount');
gr.setLimit(1);
gr.query();
if (gr.next()) {
thresholdValue = gr.amount.getReferenceValue();
}
gr = new GlideRecord('sn_risk_advanced_financial_impact_approval_threshold');
gr.addQuery('response_template', responseTemplate.getUniqueValue());
gr.addQuery('amount', '<=', thresholdValue);
gr.addQuery('is_approver_required', '1');
gr.orderBy('amount');
gr.query();
return gr;
},
_getFinancialThresholdValue: function(responseTemplate, riskEvent) {
var ruleBasedOn = responseTemplate.getValue('approval_rule_based_on');
var thresholdValue = null;
switch (ruleBasedOn) {
case "1":
thresholdValue = riskEvent.gross_amount;
break;
case "2":
thresholdValue = riskEvent.net_amount;
break;
case "3":
thresholdValue = riskEvent.expected_loss;
break;
default:
thresholdValue = riskEvent.gross_amount;
}
return parseFloat(thresholdValue.getReferenceValue());
},
_getNonFinancialThresholdValue: function(riskEvent) {
return riskEvent.getValue('non_financial_impact');
},
_shouldIssueBeAutoCreated: function(responseTemplate, thresholdValue, riskEvent) {
if (responseTemplate.getValue('automatically_create_issue') == 0) {
return false;
}
var gr = new GlideRecord('sn_risk_advanced_m2m_issue_risk_event');
gr.addQuery("risk_event", "=", riskEvent.getValue("sys_id"));
gr.query();
if (gr.hasNext()) {
return false;
}
var issueAmount = null;
if (responseTemplate.event_type == 1) {
issueAmount = parseFloat(responseTemplate.threshold_for_issue_creation_fi.getReferenceValue());
} else {
issueAmount = responseTemplate.getValue('threshold_for_issue_creation_nfi');
}
return thresholdValue >= issueAmount;
},
_shouldTaskBeAutoCreated: function(responseTemplate, thresholdValue, riskEvent) {
if (responseTemplate.getValue('automatically_create_root_cause_analysis_task') == 0) {
return false;
}
var gr = new GlideRecord('sn_risk_advanced_mitigation_task');
gr.addQuery("risk_event", riskEvent.getValue("sys_id"));
gr.addQuery("risk_event_task_type", "1");
gr.query();
if (gr.hasNext()) {
return false;
}
var taskAmount = null;
if (responseTemplate.event_type == 1) {
taskAmount = parseFloat(responseTemplate.threshold_for_task_creation_fi.getReferenceValue());
} else {
taskAmount = responseTemplate.getValue('threshold_for_task_creation_nfi');
}
return thresholdValue >= taskAmount;
},
_getThresholdValue: function(responseTemplate, riskEvent) {
if (responseTemplate.event_type == 1) {
return this._getFinancialThresholdValue(responseTemplate, riskEvent);
} else {
return this._getNonFinancialThresholdValue(riskEvent);
}
},
_canShowApproveButton :function(riskEvent) {
var approver = new GlideRecord('sysapproval_approver');
approver.addEncodedQuery('source_table=sn_risk_advanced_event^sysapproval=' + riskEvent + '^approver=' + gs.getUser().getID() + '^state=requested');
approver.query();
if(approver.hasNext()) {
approver.next();
return true;
}
return false;
},
type: 'RiskEventApprovalRulesBase'
};
Sys ID
4c2c99bb53d97300bad1ddeeff7b1276