How to get total value of survey when survey submitted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 09:01 AM
I want to display total value of survey , means if there are values as very low, low ,high , very high, want a total count
Below is the script but not working correctly, Please help!
var test = new GlideRecord('asmt_assessment_instance');
test.addQuery('sys_id','2372c745c3a50210ceb0de1d050131a9');
test.query();
if(test.next()){
gs.print(test.number);
var question = new GlideRecord('asmt_assessment_instance_question');
question.addQuery('instance',test.sys_id);
question.query();
var totalScore = 0;
var questionCount = 0;
// Loop through the questions and calculate the total score
while(question.next()) {
var row = new GlideRecord('asmt_metric_definition');
//define.addQuery('',);
row.addQuery('category','Rockefeller Capital Management Customer Satisfaction Survey');
row.addEncodedQuery('category=8403663297eff110132b37671153aff1^datatype=numericscale',question.metric);
row.query();
if(row.next()){
gs.print('in query count'+ row.getRowCount());
}
/*var response = question.getValue('response'); // Assuming response is the field where the response score is stored
if (response != '') {
totalScore += parseFloat(response);
questionCount++;
}
}
// Calculate the average
var averageScore = totalScore / questionCount;
// Print or use the average score as needed
gs.info('Average score of assessment instance questions: ' + averageScore);*/
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 09:54 AM
Hello @Asha Pathak ,
please Refer below script .i hope this will resolve your issue.
var test = new GlideRecord('asmt_assessment_instance');
test.addQuery('sys_id', '2372c745c3a50210ceb0de1d050131a9');
test.query();
if (test.next()) {
gs.print(test.number);
var question = new GlideRecord('asmt_assessment_instance_question');
question.addQuery('instance', test.sys_id);
question.query();
var categoryCounts = {
'very low': 0,
'low': 0,
'high': 0,
'very high': 0
};
// Loop through the questions and count the responses for each category
while (question.next()) {
var response = question.getValue('response'); // Assuming response is the field where the response score is stored
if (response != '') {
// Increment the count for the corresponding category
switch (response.toLowerCase()) {
case 'very low':
categoryCounts['very low']++;
break;
case 'low':
categoryCounts['low']++;
break;
case 'high':
categoryCounts['high']++;
break;
case 'very high':
categoryCounts['very high']++;
break;
default:
// Handle unexpected responses
break;
}
}
}
gs.info('Category Counts: ' + JSON.stringify(categoryCounts));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 10:20 AM - edited 04-13-2024 10:21 AM
Hi @Asha Pathak ,
There are a couple of mistakes in your code. In the "Loop through the questions and calculate the total score" code, you are querying records from the table 'asmt_metric_definition' with the condition of fields that are not even available in the table. The 'category' field is not available in the 'asmt_metric_definition' table. Instead, the table should be 'asmt_assessment_instance_question' where the 'category' field is available. Additionally, you made a mistake in row.addEncodedQuery. The method only accepts a string, whereas you are currently providing a query-like condition.
For exact error please look below code
var test = new GlideRecord('asmt_assessment_instance');
test.addQuery('sys_id','2372c745c3a50210ceb0de1d050131a9');
test.query();
if(test.next()){
gs.print(test.number);
var question = new GlideRecord('asmt_assessment_instance_question');
question.addQuery('instance',test.sys_id);
question.query();
var totalScore = 0;
var questionCount = 0;
// Loop through the questions and calculate the total score
while(question.next()) {
var row = new GlideRecord('asmt_metric_definition'); // table should be 'asmt_assessment_instance_question '
//define.addQuery('',);
row.addQuery('category','Rockefeller Capital Management Customer Satisfaction Survey');// Category field is not present in table
row.addEncodedQuery('category=8403663297eff110132b37671153aff1^datatype=numericscale',question.metric); // the addEncodedQuery method only accept string so it should be like row.addEncodedQuery('category=8403663297eff110132b37671153aff1^datatype=numericscale')
row.query();
if(row.next()){
gs.print('in query count'+ row.getRowCount());
}
/*var response = question.getValue('response'); // Assuming response is the field where the response score is stored
if (response != '') {
totalScore += parseFloat(response);
questionCount++;
}
}
// Calculate the average
var averageScore = totalScore / questionCount;
// Print or use the average score as needed
gs.info('Average score of assessment instance questions: ' + averageScore);*/
}
}
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 10:49 AM
Hi @Asha Pathak ,
I tried your problem in my PDI and I got the count - Please Refer below code
var test = new GlideRecord('asmt_assessment_instance');
test.addQuery('sys_id', 'a49d5cfd9f610200736af84bc42e704d'); // you can use any sys_id here
test.query();
if (test.next()) {
gs.print(test.number);
var question = new GlideRecord('asmt_assessment_instance_question');
question.addQuery('instance', test.sys_id);
question.query();
// Loop through the questions and calculate the total score
if(question.next()) {
var row = new GlideRecord('asmt_metric_definition');
row.addQuery('category', 'Rockefeller Capital Management Customer Satisfaction Survey');
row.addEncodedQuery('category=8403663297eff110132b37671153aff1^datatype=numericscale' + question.metric);
row.query();
if (row.next()) {
gs.print('in query count = ' + row.getRowCount());
}
}
}
Output :
Please Mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 09:32 PM
I want the following options addition