Report: How to display Survey Question/Responses in columns.

Community Alums
Not applicable

I am trying to build a report having survey questions/responses in columns. I need the columns to show Question 1, Question 2, Question 3, Question 4, Question 5 and all the answers in a single row. How can I do this?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

current.state means the business rule is running on the "asmt_metric_result" table, that has the state field. Customize your form layout to find the state field.

Sorry, I don't have gr.state in my script. It was in yours. This been a while so I was lost. Here is my working script. Please cleanup your script.

Business Rule:

find_real_file.png 

Condition: current.metric_type.getDisplayValue()=='Satisfactory Survey' && current.state == 'complete'

(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord('u_satisfactory_survey_response');
	gr.addQuery('u_survey_instance', current.sys_id);
	gr.query();
	if (!gr.next()){
		gr.initialize();
	}
		
	gr.u_survey_instance = current.sys_id;
	gr.u_metric_type = current.metric_type;
	gr.u_assigned_to =  current.user;
	gr.u_incident_number = current.trigger_id;
	gr.u_incident_assigned_to = current.related_id_1;
	gr.u_incident_assignment_group = current.related_id_2;
	
	var result = new GlideRecord('asmt_metric_result');
	result.addQuery('instance', current.sys_id);
	result.query();
	while(result.next()){
		if(result.metric == 'edfeec4f373ea20009dc65e2b3990ed4'){ //1
			gr.u_question_1 = result.metric;
			gr.u_string_value_1 = result.string_value;
			gr.u_normalized_value_1 = result.normalized_value;
			gr.u_nps_value_1 = result.nps_value;
		}
		if(result.metric == 'a1feec4f373ea20009dc65e2b3990ed5'){ //2
			gr.u_question_2 = result.metric;
			gr.u_string_value_2 = result.string_value;
			gr.u_normalized_value_2 = result.normalized_value;
			gr.u_nps_value_2 = result.nps_value;
		}
		if(result.metric == 'e1feec4f373ea20009dc65e2b3990ed5'){ //3
			gr.u_question_3 = result.metric;
			gr.u_string_value_3 = result.string_value;
			gr.u_normalized_value_3 = result.normalized_value;
			gr.u_nps_value_3 = result.nps_value;
		}
		if(result.metric == '25feec4f373ea20009dc65e2b3990ed5'){ //4
			gr.u_question_4 = result.metric;
			gr.u_string_value_4 = result.string_value;
			gr.u_normalized_value_4 = result.normalized_value;
			gr.u_nps_value_4 = result.nps_value;
		}			
		if(result.metric == '65feec4f373ea20009dc65e2b3990ed5'){ //5
			gr.u_question_5 = result.metric;
			gr.u_string_value_5 = result.string_value;
			gr.u_normalized_value_5 = result.normalized_value;
			gr.u_nps_value_5 = result.nps_value;
		}
	}	
	
	if (gr.isNewRecord()){
		gr.insert();
	}
	else {
		gr.update();
	}

})(current, previous);

View solution in original post

24 REPLIES 24

drjohnchun
Tera Guru

Below screenshot shows how you might be able to do it using the OOB reporting feature; you'll need to use the Pivot type and add filters to fit your needs.   The table to use is Assessment Instance Question [asmt_assessment_instance_question] (or the legacy Survey Response [survey_response] table, if you're using it).



You may also look into Performance Analytics Pivot Tables.



Another option is to export the data and use Excel to pivot the data, using either the legacy Pivot or Power Pivot.



I don't think you can create a database view for this since you can't turn the questions into columns using the built-in capabilities.



find_real_file.png



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


Community Alums
Not applicable

I designed a separate table for each survey because the question #s varies for each survey. I created a business rule for the new survey response records. For existing survey records, created a background script (to copy all response data into new table).

 

Columns go like this..

 

find_real_file.pngfind_real_file.png

 

 

 

Business rule:

 

==========================================================

 

 

 

(function executeRule(current, previous /*null when async*/) {

 

 

 

      var gr = new GlideRecord('u_company_satisfactory_survey_response');

 

      gr.addQuery('u_survey_instance', current.sys_id);

 

      gr.query();

 

      if (!gr.next()){

 

              gs.log('-----------------' + gr.u_survey_instance.getDisplayValue());

 

              gr.initialize();

 

      }

 

             

 

      gr.u_survey_instance = current.sys_id;

 

      gr.u_metric_type = current.metric_type;

 

      gr.u_assigned_to =   current.user;

 

      gr.u_incident_number = current.trigger_id;

 

      gr.u_incident_assigned_to = current.related_id_1;

 

      gr.u_incident_assignment_group = current.related_id_2;

 

     

 

      if (gr.state == 'complete'){

 

      gs.log('---------------complete');

 

              var result = new GlideRecord('asmt_metric_result');

 

              result.addQuery('instance', gr.u_survey_instance);

 

              result.query();

 

              while(result.next()){

 

                      if(result.metric == 'edfeec4f373ea20009dc65e2b3990ed4'){ //1

 

                              gr.u_question_1 = result.metric;

 

                              gr.u_string_value_1 = result.string_value;

 

                              gr.u_normalized_value_1 = result.normalized_value;

 

                      }

 

                      if(result.metric == 'a1feec4f373ea20009dc65e2b3990ed5'){ //2

 

                              gr.u_question_2 = result.metric;

 

                              gr.u_string_value_2 = result.string_value;

 

                              gr.u_normalized_value_2 = result.normalized_value;

 

                      }

 

                      if(result.metric == 'e1feec4f373ea20009dc65e2b3990ed5'){ //3

 

                              gr.u_question_3 = result.metric;

 

                              gr.u_string_value_3 = result.string_value;

 

                              gr.u_normalized_value_3 = result.normalized_value;

 

                      }

 

                      if(result.metric == '25feec4f373ea20009dc65e2b3990ed5'){ //4

 

                              gr.u_question_4 = result.metric;

 

                              gr.u_string_value_4 = result.string_value;

 

                              gr.u_normalized_value_4 = result.normalized_value;

 

                      }                      

 

                      if(result.metric == '65feec4f373ea20009dc65e2b3990ed5'){ //5

 

                              gr.u_question_5 = result.metric;

 

                              gr.u_string_value_5 = result.string_value;

 

                              gr.u_normalized_value_5 = result.normalized_value;

 

                      }

 

              }

 

      }

 

     

 

      if (gr.isNewRecord()){

 

              gr.insert();

 

      }

 

      else {

 

              gr.update();

 

      }

 

 

 

})(current, previous);

 

OK. That's what I expected. I was thinking you had some magical script that would build a table out with all the unique questions from each survey. 😃 I appreciate you sharing the code.


Also, which table was this running against? Metric Result?


Hi,

I tried to use the above BR. But in my case, while it is successfully creating a new record in custom table, it does not copy the responses to the individual fields. They just stay empty.