Addition of metric values using automated scripted factors.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 01:49 PM - edited 12-07-2023 01:54 PM
Addition of questionnaire responses and multiple them by weightages in Advanced Risk Inherent Assessment using Automated scripted factors.
We are using advanced risk functionality for one of the clients and in the Risk Identification record, inherent risk assessment stage once the assessment got created all the Manual/Automated/Group factors which we have configured for the Risk Assessment Methodology will be displayed. In our case we are using Automated scripted factors because inherent risk needs to be populated automatically from the responses submitted at information gathering stage.
We have added new categories and added new metrics in the assessment metric table. For each question I have Values as Yes or No and weightage is different for each question. I'm trying to achieve this using Automated scripted factor which I'm unable to achieve.
Example:
1. Question-1 (Weight = 2)
Responses --> Yes ( value =1), No (Value = 0)
2. Question-2 (Weight = 3)
Responses --> Yes ( value =1), No (Value = 0)
3. Question-3 (Weight = 4)
Responses --> Yes ( value =1), No (Value = 0)
If user is selecting Yes in question 1 and question 2, No in Question 3 then my output should be -
(2*1)+(3*1)+(4*0)=5(weightage of question 1*response value + weightage of question 2*response value +weightage of question 3*response value)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 03:44 PM
Is this a question?
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 09:32 PM
Yes, it is
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 09:48 PM
HI @SUSMITHA KRISHN ,
I trust you are doing great.
To address your requirement in ServiceNow for calculating the inherent risk based on questionnaire responses and their respective weightages, you can utilize a scripted factor within the Advanced Risk functionality.
function calculateRiskScore(assessmentRecord) {
// Initialize total score
var totalScore = 0;
// Define questions with their weightages
var questions = [
{ weight: 2, response: getResponseValue(assessmentRecord, 'question1') },
{ weight: 3, response: getResponseValue(assessmentRecord, 'question2') },
{ weight: 4, response: getResponseValue(assessmentRecord, 'question3') }
];
// Iterate over each question
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
// Calculate score for each question
totalScore += question.weight * question.response;
}
return totalScore;
}
function getResponseValue(assessmentRecord, questionField) {
// Retrieve response value from the assessment record
// Assuming 'Yes' = 1 and 'No' = 0
var response = assessmentRecord.getValue(questionField);
return (response === 'Yes') ? 1 : 0;
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:00 PM - edited 12-12-2023 12:06 PM
Hi @Amit Gujarathi ,
Thank you for the response. You surely gave me an idea on how the calculation can be done. But I don't want to directly define questions and weightages in the script. So, I wrote the script in below way and it's working fine for me.
var supportingFields = ['name', 'question', 'weight'];//add field names of question, value and weight
var data = '"Metric","response"\r\n';
var asmtInstance = new GlideRecord('sample1');
//add conditions
asmtInstance.query();
if (asmtInstance.next()) {
var questionResponses = new GlideRecord('sample2');
//add conditions for getting assessment name and category
questionResponses.query();
var total = 0;
while (questionResponses.next()) {
for (var j = 0; j < supportingFields.length; j++) {
var question = questionResponses.getElement(supportingFields[j]).getDisplayValue();
if (question.indexOf(',') != -1 || question.indexOf('"') != -1) {
question = question.replace(/"/g, '""');
data = data + '"' + question + '"';
} else {
data = data + question;
}
if (j != supportingFields.length - 1)
data = data + ",";
}
data = data + "\r\n";
total+=(questionResponses.question*questionResponses.weight);
}
Thanks,
Susmitha