Change Risk assessment does not copy attachments when attachment question is addedDescriptionChange Risk assessment does not copy attachments when attachment question is added and subsequent Change Risk Assessments are taken.Steps to Reproduce 1. Modify the OOB Change Risk Assessment adding an attachment type question2. Create a normal type change request.3. Click Risk Assessment and attach a document.4. Submit Risk Assessment.5. Click Risk Assessment again6. Select yes on dialog informing you that you have already completed a risk assessment. Expected: the previous attachments are maintainedActual: the previous attachments are missingWorkaroundModify Script include: ChangeRiskAsmt sysId: 884c5254d7233200532c24837e61032c as follows: var ChangeRiskAsmt = Class.create(); ChangeRiskAsmt.prototype = Object.extendsObject(ChangeRiskAsmtSNC, { deleteAsmtInstance: function(asmtInstanceSysId) { if (!asmtInstanceSysId) return false; var changeRequestSysId = ""; var asmtInstanceGr = new GlideRecord(this.ASMT_INSTANCE); if (!asmtInstanceGr.get(asmtInstanceSysId)) return false; if (asmtInstanceGr.getValue('user') !== gs.getUserID()) return false; var state = asmtInstanceGr.getValue("state"); var className = asmtInstanceGr.metric_type.getRefRecord().getValue("sys_class_name"); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[deleteAsmtInstance] state: " + state + " className: " + className); // Do NOT delete completed assessmentInstance or not a change_risk_asmt if (state === this.COMPLETE || className !== this.CHANGE_RISK_ASMT) return false; if (asmtInstanceGr.isValidRecord()) { this._deleteAttachmentRecords(asmtInstanceSysId); this._deleteAnswerRecords(asmtInstanceSysId); asmtInstanceGr.deleteRecord(); var chgRiskWorkerGr = new GlideRecord("chg_risk_asmt_worker"); chgRiskWorkerGr.addQuery("asmt_assessment_instance", asmtInstanceSysId); chgRiskWorkerGr.query(); chgRiskWorkerGr.deleteMultiple(); } if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[deleteAsmtInstance] asmtInstanceSysId: " + asmtInstanceSysId); return true; }, _deleteAttachmentRecords: function(asmtInstanceSysId) { if (!asmtInstanceSysId) return; var attachmentGr = new GlideRecord('sys_attachment'); attachmentGr.addQuery('table_name', this.ASMT_INSTANCE_QUES); attachmentGr.addQuery('table_sys_id', 'IN', this._getAttachmentQuestions(asmtInstanceSysId)); attachmentGr.deleteMultiple(); }, _getAttachmentQuestions: function(asmtInstanceSysId) { var sysIds = []; if (!asmtInstanceSysId) return sysIds; var gr = new GlideRecord(this.ASMT_INSTANCE_QUES); gr.addQuery(this.INSTANCE, asmtInstanceSysId); gr.addQuery('metric.datatype', 'attachment'); gr.addQuery('instance.metric_type.sys_class_name', this.CHANGE_RISK_ASMT); gr.addQuery('instance.state', this.NOT, this.COMPLETE); gr.query(); while (gr.next()) sysIds.push(gr.getUniqueValue()); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_getAttachmentQuestions] sysIds: " + sysIds); return sysIds; }, _copyAsmt: function(previousAsmtInstanceSysId, asmtInstanceSysId) { if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_copyAsmt] previousAsmtInstanceSysId: " + previousAsmtInstanceSysId + " asmtInstanceSysId: " + asmtInstanceSysId); if (!previousAsmtInstanceSysId || !asmtInstanceSysId) return; // For new assessment instance remove answers created by API this._deleteAnswerRecords(asmtInstanceSysId); var previousAnswerGr = new GlideRecord(this.ASMT_INSTANCE_QUES); previousAnswerGr.addQuery(this.INSTANCE, previousAsmtInstanceSysId); previousAnswerGr.query(); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_copyAsmt] previousAnswerGr table: " + previousAnswerGr.getTableName() + " encodedQuery: " + previousAnswerGr.getEncodedQuery()); while (previousAnswerGr.next()) { var copiedAnswerGr = new GlideRecord(this.ASMT_INSTANCE_QUES); var previousAnswerSysId = previousAnswerGr.getUniqueValue(); if (!copiedAnswerGr.get(previousAnswerSysId)) continue; copiedAnswerGr.setValue(this.INSTANCE, asmtInstanceSysId); // Inserting a previous questions creates a copy var copiedAnswerSysId = copiedAnswerGr.insert(); if (previousAnswerGr.metric.datatype + '' === 'attachment') this._copyAttachments(previousAsmtInstanceSysId, previousAnswerSysId, copiedAnswerSysId); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_copyAsmt] copiedAnswerSysId: " + copiedAnswerSysId + " instance: " + copiedAnswerGr.instance.getDisplayValue() + " previous instance: " + previousAnswerGr.instance.getDisplayValue()); } }, _copyAttachments: function(previousAsmtInstanceSysId, previousAnswerSysId, copiedAnswerSysId) { if (!previousAsmtInstanceSysId || !previousAnswerSysId || !copiedAnswerSysId) return; var previousMetricSysIds = []; var metricResultGr = new GlideRecord(this.ASMT_METRIC_RESULT); metricResultGr.addQuery(this.INSTANCE, previousAsmtInstanceSysId); metricResultGr.addQuery('instance_question', previousAnswerSysId); metricResultGr.addQuery('metric.datatype', 'attachment'); metricResultGr.query(); while (metricResultGr.next()) { previousMetricSysIds.push(metricResultGr.getUniqueValue()); } if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_copyAttachments] previousMetricSysIds: " + previousMetricSysIds); if (!previousMetricSysIds || previousMetricSysIds.length < 1) return; previousMetricSysIds.forEach(function(previousMetricSysId) { var attachmentSysIds = GlideSysAttachment.copy(this.ASMT_METRIC_RESULT, previousMetricSysId, this.ASMT_INSTANCE_QUES, copiedAnswerSysId); if (this._log.atLevel(global.GSLog.DEBUG)) this._log.debug("[_copyAttachments] attachmentSysIds: " + attachmentSysIds); }, this); }, type: 'ChangeRiskAsmt' }); Related Problem: PRB1699111