Brian Lancaster
Tera Sage

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.

find_real_file.png

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();
Comments
Chris Doernbra1
Mega Guru

Thank you so much for taking the time to post this. This is exactly what I needed and it works beautifully.

Adam43
Tera Contributor

Question- in your first step of 'setup input values for all questions and the record ID'- what table were you creating those on?  or is that a step in ATF i'm not aware of?

I'm trying to replicate this, but trying to figure out where to start.

Brian Lancaster
Tera Sage

This is part of creating the custom test step. Once you go to Automated Test Framework (ATF) > Administration > Step Configurations. You then have to create a new test step configuration. The input will be at the bottom of the test step configuration once you save it the first time.

Adam43
Tera Contributor

Thanks so much!  I'm excited to start working on this.

So if i'm customizing to our version of the Risk Assessment, I should make these input column names specific to our questions, right?  and the response.values to the corresponding answers I want?

Brian Lancaster
Tera Sage

correct and you should always verify the sys_id of your assessment. Which you can find my going to change_risk_asmt table. This is for the line of code that start with var metricType.

Adam43
Tera Contributor

getting an error on the ChangeRiskAsmtSNC script include- 

ERROR Test failed: ChangeRiskAsmtSNC undefined, maybe missing global qualifier

i think because ATF is scoped and the script include is global, but i'm not sure what the syntax needs to be to call that global include?  but the scriptinclude does say 'accessible from all scopes'.

 

I tried..... 


var assessmentDetails = new ChangeRiskAsmtSNC().global_createAsmt(metricType, justCreatedGR.sys_id, justCreatedGR.assigned_to);

Brian Lancaster
Tera Sage

If you are in a scope other then global you will need to add global. Infront of all script include calls.

var assessmentDetails = new global.ChangeRiskAsmtSNC()._createAsmt(metricType, justCreatedGR.sys_id, justCreatedGR.assigned_to);

Adam43
Tera Contributor

SUCCESS!  Thanks so much for the help- I'm commenting this article in my script- SUPER helpful!

Brian Lancaster
Tera Sage

Thanks for the feedback. I have also updated the code to include global. in that one line since I'm doing that in other script include calls. You are not the first person to need this answer.  

mv
Tera Contributor

Hi Brain, I'm trying to write a complete ATF for Normal change request. I have created sequence of test steps from creating new CR record. As you have mentioned I have created a new record in test step configuration table . How do I relate the the test step in config table to the one I have created in the test table. Kindly suggest. 

Brian Lancaster
Tera Sage

Once you create a record in the step configuration you will now be able to use it as a test step. So you would just need to add a new test step in test and search for the one you created.

Community Alums
Not applicable

This blog is my savior. Based on this I created a custom step for risk assessment for change ticket.

Thank you so much Brian.

Just to add one of my observation related below method call where assigned_to should be passed with non-null value else system won't evaluate the risk value:

global.ChangeRiskAsmtSNC()._createAsmt(metricType, justCreatedGR.sys_id, justCreatedGR.assigned_to)

Brian Lancaster
Tera Sage

correct assigned to cannot be null. The system needs to know who is using the assessment. 

Wei_ Ling
Tera Guru

 

Thank you very much for your article, it was very effective for me

NMiel
Tera Guru

Thank you, this article was very helpful!  I was able to successfully create the custom test step configuration and configured the input variables to reference the Metric.  Now out tests can be configured to use the Risk Assessment step without having to know the values of each Assessment Metric Definition.  This is going to be very useful for our upgrade testing!

Version history
Last update:
‎12-07-2021 05:16 PM
Updated by: