Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get total value of survey when survey submitted

Asha Pathak
Tera Contributor

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);*/
    }

 }

5 REPLIES 5

Abhijeet_Pawar
Tera Guru
Tera Guru

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));
}

 

Astik Thombare
Tera Sage
Tera Sage

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

 

 

Community Alums
Not applicable

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 : 

SarthakKashya2_1-1713030512952.png

Please Mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

 

I want the following options addition

AshaPathak_0-1713069146782.png