Change Risk Assessment copying wrong completed assessment instance if original change risk assessment no longer matches new Change Risk AssessmentDescriptionChange Risk Assessment copying wrong completed assessment instance if original change risk assessment no longer matches new Change Risk AssessmentSteps to Reproduce install Change Risk Assessment, including demo dataCopy the OOB demo data Change Risk Assessment, and change the condition and metric category, to match category=hardware, and name to Copy Change Risk Assessment.Add an additional question to the Copy Change Risk Assessment.Modify the Change Risk Assessment condition and metric category, to NOT match category=hardware.Create a change request with category=hardwareClick on Risk Assessment and note that "Copy Change Risk Assessment" shows. Submit the Change Risk Assessment.Alter the Change Request category to Network and save.Click Risk Assessment Expected:A new empty Change Risk Assessment form shows up. Actual:Modal saying "You have already completed the risk assessment. Would you like to edit your existing risk assessment?"clicking yes, copies the completed but not-matching "Copy Change Risk Assessment"WorkaroundModify Script Include: ChangeRiskAsmt sys_id: 884c5254d7233200532c24837e61032c Add the following functions: _getTableName: function(tableName) { tableName = JSUtil.nil(tableName) ? this.CHANGE_REQUEST : tableName + ''; if (tableName !== this.CHANGE_REQUEST) { var tableHierarchy = (new TableUtils(tableName)).getTables(); if (tableHierarchy.indexOf(this.CHANGE_REQUEST) < 0) { if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_getTableName] table: " + tableName + " is not or does not extend change_request"); return ""; } } return tableName; }, invokeAssessment: function(data) { var asmt = { "hasAsmt": false, "asmtComplete": false }; if (!data) return asmt; var userSysId = data.userId || gs.getUserID(); var changeRequestSysId = data.sysId || ""; var tableName = data.tableName || ""; var changeRiskAsmtSysId = data.riskAsmtSysId || ""; var copyAsmt = false; if (data && data.copyAsmt) copyAsmt = data.copyAsmt + "" === "true"; var changeRiskAsmtSysIds; var changeGr = new GlideRecordSecure(tableName); if (!changeGr.get(changeRequestSysId) || !changeGr.canWrite()) return asmt; if (!changeRiskAsmtSysId && (changeRequestSysId && tableName)) { changeRiskAsmtSysIds = this._getChangeRiskAsmtsByConditions(changeRequestSysId, tableName); // Get first Change Risk Assessment but in future handle all if (changeRiskAsmtSysIds.length > 0) changeRiskAsmtSysId = changeRiskAsmtSysIds[0]; } if (!userSysId || !changeRequestSysId || !changeRiskAsmtSysId) return asmt; if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[invokeAssessment] userSysId: " + userSysId + " changeRequestSysId: " + changeRequestSysId + " changeRiskAsmtSysId: " + changeRiskAsmtSysId); var asmtInstanceSysId = ""; var incompleteAsmtInstanceSysIds = this._getAsmtInstancesByTaskId(changeRequestSysId, this.INCOMPLETE, changeRiskAsmtSysIds); if (incompleteAsmtInstanceSysIds.length > 0) asmtInstanceSysId = incompleteAsmtInstanceSysIds[0]; else { var completeAsmtInstanceSysIds = this._getAsmtInstancesByTaskId(changeRequestSysId, this.COMPLETE, changeRiskAsmtSysIds); if (completeAsmtInstanceSysIds.length > 0 && !copyAsmt) asmt.asmtComplete = true; else { var previousAsmtInstanceSysId = ""; if (completeAsmtInstanceSysIds.length > 0) previousAsmtInstanceSysId = completeAsmtInstanceSysIds[0]; asmtInstanceSysId = this._createAsmt(changeRiskAsmtSysId, changeRequestSysId, userSysId); if (copyAsmt && previousAsmtInstanceSysId) this._copyAsmt(previousAsmtInstanceSysId, asmtInstanceSysId); } } asmt.hasAsmt = true; asmt.changeRequestSysId = changeRequestSysId; asmt.asmtInstanceSysId = asmtInstanceSysId; asmt.changeRiskAsmtSysId = changeRiskAsmtSysId; asmt.riskAsmtName = this._getAssessmentName(changeRiskAsmtSysId) || ""; if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[invokeAssessment] asmt: " + JSON.stringify(asmt)); return asmt; }, _getAsmtInstancesByTaskId: function(taskId, state, matchingAsmtSysIds) { state = this._isAsmtComplete(state) ? this.COMPLETE : this.INCOMPLETE; if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_getAsmtInstancesByTaskId] taskId: " + taskId + " state: " + state); if (!taskId) return []; matchingAsmtSysIds = !matchingAsmtSysIds ? this._getChangeRiskAsmtsByConditions(taskId) : matchingAsmtSysIds; var asmtInstances = []; var gr = new GlideRecord(this.ASMT_INSTANCE); gr.addQuery("task_id", taskId); gr.addQuery("metric_type", "IN", matchingAsmtSysIds); if (state === this.COMPLETE) gr.addQuery("state", this.COMPLETE); else gr.addQuery("state", this.NOT, this.COMPLETE); gr.addQuery("metric_type.sys_class_name", this.CHANGE_RISK_ASMT); gr.orderByDesc("sys_created_on"); gr.query(); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_getAsmtInstancesByTaskId] encodedQuery: " + gr.getEncodedQuery()); while (gr.next()) asmtInstances.push(gr.getUniqueValue()); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_getAsmtInstancesByTaskId] " + state + " assessment instances: " + asmtInstances.length + "" + JSON.stringify(asmtInstances)); return asmtInstances; }, _calculateAsmtRisk: function(changeRequestGr) { changeRequestGr = changeRequestGr || this.changeRequestGr; var tableName = changeRequestGr.getTableName(); var sysId = changeRequestGr.getUniqueValue(); var matchingAsmtSysIds = this._getChangeRiskAsmtsByConditions(sysId, tableName); var asmtInstances = this._getAsmtInstancesByTaskId(sysId, this.COMPLETE, matchingAsmtSysIds); var asmtScore = this.calcAsmtScore(asmtInstances[0]); var riskAssessment = this._getRiskAsmt(matchingAsmtSysIds, asmtScore); if (riskAssessment.risk && riskAssessment.risk.value) riskAssessment.msg = gs.getMessage("Risk Assessment calculated: {0}; Risk: {1}{2}{3}", [ riskAssessment.name.display_value, new RiskCalculator().getStartColorTag(), riskAssessment.risk.display_value || riskAssessment.risk.value, new RiskCalculator().getEndColorTag() ]); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_calculateAsmtRisk] riskAssessment: " + JSON.stringify(riskAssessment)); return riskAssessment; }, Related Problem: PRB1635980