[How To] ATF Server Side Script Step for Change Risk Assessment
After trying another article post and having major problem. I asked a question on the community which eventually got to support case. I decided to put my resolution here and how I configured the test step.
First thing I did was setup input values for all questions and the record ID I wanted to put the assessment against. I made the fields that represented the questions a integer field with choices so I could set what I wanted for each test. Note that the field type is integer as the backend value in assessment is a number. These will need to be configured based on your questions. We are using the OOB Demo assessment for change.
Per ServiceNow you should not use the SNC.AssessmentCreation().createAssessments as it can create multiple assessments so they had me switch to calling the ChangeRiskAsmtSNC() script include. Below is the final code.
(function executeStep(inputs, outputs, stepResult, timeout) {
//set variables for all change inputs
var changeID = inputs.u_record.toString();
var bis_svc = inputs.u_ci_business_service;
var complex = inputs.u_complexity_of_change;
var dificult = inputs.u_difficult_to_revert;
var redundancy = inputs.u_redundancy_plan;
var verify = inputs.u_verification_change;
var justCreatedGR = new GlideRecord('change_request');
if (justCreatedGR.get(changeID)) {//get change record to pull fields o who will take they survey.
//create assessment
var metricType = '2e3ab7f1d7033200532c24837e61034b'; //sys_id of the metric type you can find in change_risk_asmt table
var assessmentDetails = new global.ChangeRiskAsmtSNC()._createAsmt(metricType, justCreatedGR.sys_id, justCreatedGR.assigned_to);
var asmtInstanceSysId = assessmentDetails; //survey sys_id
//set response in assessment questions
var setResponse = new GlideRecord('asmt_assessment_instance_question');
setResponse.addQuery('instance', asmtInstanceSysId);
setResponse.query();
while (setResponse.next()) { //set assessment answers
if (setResponse.metric.name == 'Complexity of change') {
setResponse.value = complex;
} else if (setResponse.metric.name == 'Difficult to revert') {
setResponse.value = dificult;
} else if (setResponse.metric.name == 'Verification of change') {
setResponse.value = verify;
} else if (setResponse.metric.name == 'Affect critical CI or Business Service') {
setResponse.value = bis_svc;
} else {
setResponse.value = redundancy;
}
setResponse.update();
}
//set assessment to complete and link to task id
var justUpdatedInst = new GlideRecord('asmt_assessment_instance');
if (justUpdatedInst.get(asmtInstanceSysId)) {
justUpdatedInst.state = "complete";
justUpdatedInst.task_id = changeID;
justUpdatedInst.update();
}
//calculate assessment result and update change record with calculated risk
var calculatedRisk = new global.RiskCalculator(justCreatedGR).evaluateRiskImpact();
if (calculatedRisk.riskEvaluation.risk.value == 4 || calculatedRisk.riskEvaluation.risk.value == 3 ||
calculatedRisk.riskEvaluation.risk.value == 2) {//Check is asessment returned one of the expected values
stepResult.setOutputMessage("calculatedRisk.risk " + calculatedRisk.riskEvaluation.risk.value);
justCreatedGR.risk = calculatedRisk.riskEvaluation.risk.value;
justCreatedGR.update();
}
stepResult.setSuccess();//successful test
return;
} else {
stepResult.setOutputMessage("Failed to calculate risk");
stepResult.setFailed(); // fail the step
}
}(inputs, outputs, stepResult, timeout));
Edit: I have now created a script for the Description generator section.
function generateDescription() {//Note that you will need to update all imputs to the name of your imputs if they are different then the mones listed below.
// the global variable 'step' represents the current glide record
var description = "Set Risk Assessment for " + step.inputs.u_record.getDisplayValue() + "\n";
// your code here
description += "With the following Values: \n\n";
description += "Does the change affect a critical CI or Business service?: " + step.inputs.u_ci_business_service.getDisplayValue() + "\n";
description += "How Complex is the Change?: " + step.inputs.u_complexity_of_change.getDisplayValue() + "\n";
description += "How difficult is the change to back out or revert?: " + step.inputs.u_difficult_to_revert.getDisplayValue() + "\n";
description += "Is there a redundancy plan in place?: " + step.inputs.u_redundancy_plan.getDisplayValue() + "\n";
description += "How difficult is it to verify the change was successful?: " + step.inputs.u_verification_change.getDisplayValue() + "\n";
return description;
}
generateDescription();
https://www.servicenow.com/community/developer-articles/how-to-atf-server-side-script-step-for-change-risk-assessment/ta-p/2318829