function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kenneth KimbrellKenneth Kimbrell 

What is the best way to count records for questions that have a yes or no answer whey querying and using a wrapper class?

Here is what I am trying to accomplish:

Not sure the best way to ask this. But essentially I have a survey for instructors that a student submits during the mid-term. Each survey submission is of course a different record but the instructor and class would be the same. When querying results I only want to show one record for that teacher and class not multiple records from the same teacher and class. Also I want to count records that have a yes or no answer within a specific custom field. An example:

//Instrustor             //Registered                   //NumofYes      //NumOfNo    //total response
Course Becky         Gosky English Composition              1                5               6
Bobbie Strother      Developmental Psychology               1                6               7
Bobbie Strother      Anatomy & Physiology                   1                6               7
Bruce Priddy         Developmental Psychology               1                8               9
Christy Saladino     Developmental Psychology               3                7               10 
Christy Saladino     NCLEX Preparation                      1                9               10

Righ now my current code works, but only counts all responses. I was wondering what would be the best way to go about this without having to do multiple queries. My current method looks like this:

//Start a survey wrapper class
    public class SurveyWrapper{ 
        public End_Of_Class_Survey__c surveyObj {get;set;}
        public integer responseCount {get;set;}
        
        public SurveyWrapper(){
            surveyObj = new End_Of_Class_Survey__c();
            responseCount = 0;
        }
    }
    //Constructor Method
    public CLS_InstructorMidSurvey(){
        instructorName = '';
        courseName = '';
        getListOfInstructorAndCourses();
    }
    //main wrapper class method to getListOfInstructorAndCoursest
    public list<SurveyWrapper> getListOfInstructorAndCourses(){
   listOfSurveyData = new list<End_Of_Class_Survey__c>([SELECT Id, Question_1_Mid_Survey__c, Question_1B_Mid_Survey__c, Question_1A_MidSurvey__c, Instructor__c, Registered_Course__c 
                                                             FROM End_Of_Class_Survey__c 
                                                             WHERE Registered_Course__c != null AND Instructor__c != null 
                                                             ORDER BY Instructor__c]);
        listOfInstructorAndCourses = new list<SurveyWrapper>();
        mapOfSurveyData = new map<string, list<End_Of_Class_Survey__c>>();
        
        /*loop through list and find key values that contain both the instructor and the registered course.
         If map does contains(key) then get the key and add it to the lstSurveyData.
         Add the entire listOfSurveyData to the lstSurveyData
         Finally make the string a unique key in the map and add the lstSurveyData as its value.
         This will allow for a unique list and no repeat of records pertaining to the same course and instructor*/
        for(End_Of_Class_Survey__c e: listOfSurveyData){
            lstSurveyData = new list<End_Of_Class_Survey__c>();
            string key = e.Instructor__c + '-' + e.Registered_Course__c;
            
            if(mapOfSurveyData.containsKey(key)){
                lstSurveyData = mapOfSurveyData.get(key);
            }
            
            lstSurveyData.add(e);
            mapOfSurveyData.put(key,lstSurveyData);
        }
        //loop through the map's keyset
        for(string key: mapOfSurveyData.keySet()){
            //create a new wrapper object and add the values to the wrapper class
            SurveyWrapper wrapperObj = new SurveyWrapper();
            wrapperObj.surveyObj = mapOfSurveyData.get(key)[0];
            wrapperObj.responseCount = mapOfSurveyData.get(key).size();
            listOfInstructorAndCourses.add(wrapperObj);  
        }
        return listOfinstructorAndCourses;
    }
Any help would be greatly appreciated :)
Best Answer chosen by Kenneth Kimbrell
Dushyant SonwarDushyant Sonwar
Hi Kenneth ,
As you said Question_1_Mid_Survey__c stores the yes no value.

Please do the following changes in your class.

1) Create a variable in your wrapper class to store positive and negative responses for your survey.
public class SurveyWrapper{ 
        public End_Of_Class_Survey__c surveyObj {get;set;}
        public integer responseCount {get;set;}
	    public Integer responsePositiveCount{get;set;}
        public Integer responseNegativeCount{get;set;}
		
        public SurveyWrapper(){
            surveyObj = new End_Of_Class_Survey__c();
            responseCount = 0;
	        responsePositiveCount = 0;
            responseNegativeCount = 0;
        }
}
2) Calculate the postive and negative response for the survey.
//main wrapper class method to getListOfInstructorAndCoursest
    public list<SurveyWrapper> getListOfInstructorAndCourses(){
   listOfSurveyData = new list<End_Of_Class_Survey__c>([SELECT Id, Question_1_Mid_Survey__c, Question_1B_Mid_Survey__c, Question_1A_MidSurvey__c, Instructor__c, Registered_Course__c 
                                                             FROM End_Of_Class_Survey__c 
                                                             WHERE Registered_Course__c != null AND Instructor__c != null 
                                                             ORDER BY Instructor__c]);
        listOfInstructorAndCourses = new list<SurveyWrapper>();
        mapOfSurveyData = new map<string, list<End_Of_Class_Survey__c>>();
        
        /*loop through list and find key values that contain both the instructor and the registered course.
         If map does contains(key) then get the key and add it to the lstSurveyData.
         Add the entire listOfSurveyData to the lstSurveyData
         Finally make the string a unique key in the map and add the lstSurveyData as its value.
         This will allow for a unique list and no repeat of records pertaining to the same course and instructor*/
		 
		 
		Map<String, Integer> mapOfSurveyPositiveResponse = new Map<String, Integer>();
		Integer postiveResponse;		
		for(End_Of_Class_Survey__c e: listOfSurveyData){
            lstSurveyData = new list<End_Of_Class_Survey__c>();
            string key = e.Instructor__c + '-' + e.Registered_Course__c;
            postiveResponse = 0;
            if(mapOfSurveyData.containsKey(key)){
                lstSurveyData = mapOfSurveyData.get(key);								
            }			
			if(mapOfSurveyPositiveResponse.contains(key)){
				postiveResponse = mapOfSurveyPositiveResponse.get(key) + 1;
			}
            if(e.Question_1_Mid_Survey__c == 'Yes'){
				postiveResponse++;
				
			}
            lstSurveyData.add(e);
            mapOfSurveyData.put(key,lstSurveyData);	
			mapOfSurveyPositiveResponse.put(key , postiveResponse);			
        }
		
		
        //loop through the map's keyset
        for(string key : mapOfSurveyData.keySet()){
            //create a new wrapper object and add the values to the wrapper class
            SurveyWrapper wrapperObj = new SurveyWrapper();
            wrapperObj.surveyObj = mapOfSurveyData.get(key)[0];
            wrapperObj.responseCount = mapOfSurveyData.get(key).size();
			wrapperObj.responsePositiveCount = mapOfSurveyPositiveResponse.get(key);
			wrapperObj.responseNegativeCount =  mapOfSurveyData.get(key).size() - mapOfSurveyPositiveResponse.get(key);
            listOfInstructorAndCourses.add(wrapperObj);  
        }
        return listOfinstructorAndCourses;
    }


 

All Answers

Kenneth KimbrellKenneth Kimbrell
The yes and no, is a field called "Question_1_Mid_Survey__c" which is a picklist datatype of yes or no. So basically I would want to maintain this unique list and also find a way to count how many students replied with yes and how many replied with no for this question and for each teacher course survey. It has been an ongoing project that I can't seem to get a full control of. So any advice from some veterans out there would be greatly appreciated. Dushyant Sonwar helped me immensely and I am pretty close just need to get over this final hurdle. Thanks!
Dushyant SonwarDushyant Sonwar
Hi Kenneth ,
As you said Question_1_Mid_Survey__c stores the yes no value.

Please do the following changes in your class.

1) Create a variable in your wrapper class to store positive and negative responses for your survey.
public class SurveyWrapper{ 
        public End_Of_Class_Survey__c surveyObj {get;set;}
        public integer responseCount {get;set;}
	    public Integer responsePositiveCount{get;set;}
        public Integer responseNegativeCount{get;set;}
		
        public SurveyWrapper(){
            surveyObj = new End_Of_Class_Survey__c();
            responseCount = 0;
	        responsePositiveCount = 0;
            responseNegativeCount = 0;
        }
}
2) Calculate the postive and negative response for the survey.
//main wrapper class method to getListOfInstructorAndCoursest
    public list<SurveyWrapper> getListOfInstructorAndCourses(){
   listOfSurveyData = new list<End_Of_Class_Survey__c>([SELECT Id, Question_1_Mid_Survey__c, Question_1B_Mid_Survey__c, Question_1A_MidSurvey__c, Instructor__c, Registered_Course__c 
                                                             FROM End_Of_Class_Survey__c 
                                                             WHERE Registered_Course__c != null AND Instructor__c != null 
                                                             ORDER BY Instructor__c]);
        listOfInstructorAndCourses = new list<SurveyWrapper>();
        mapOfSurveyData = new map<string, list<End_Of_Class_Survey__c>>();
        
        /*loop through list and find key values that contain both the instructor and the registered course.
         If map does contains(key) then get the key and add it to the lstSurveyData.
         Add the entire listOfSurveyData to the lstSurveyData
         Finally make the string a unique key in the map and add the lstSurveyData as its value.
         This will allow for a unique list and no repeat of records pertaining to the same course and instructor*/
		 
		 
		Map<String, Integer> mapOfSurveyPositiveResponse = new Map<String, Integer>();
		Integer postiveResponse;		
		for(End_Of_Class_Survey__c e: listOfSurveyData){
            lstSurveyData = new list<End_Of_Class_Survey__c>();
            string key = e.Instructor__c + '-' + e.Registered_Course__c;
            postiveResponse = 0;
            if(mapOfSurveyData.containsKey(key)){
                lstSurveyData = mapOfSurveyData.get(key);								
            }			
			if(mapOfSurveyPositiveResponse.contains(key)){
				postiveResponse = mapOfSurveyPositiveResponse.get(key) + 1;
			}
            if(e.Question_1_Mid_Survey__c == 'Yes'){
				postiveResponse++;
				
			}
            lstSurveyData.add(e);
            mapOfSurveyData.put(key,lstSurveyData);	
			mapOfSurveyPositiveResponse.put(key , postiveResponse);			
        }
		
		
        //loop through the map's keyset
        for(string key : mapOfSurveyData.keySet()){
            //create a new wrapper object and add the values to the wrapper class
            SurveyWrapper wrapperObj = new SurveyWrapper();
            wrapperObj.surveyObj = mapOfSurveyData.get(key)[0];
            wrapperObj.responseCount = mapOfSurveyData.get(key).size();
			wrapperObj.responsePositiveCount = mapOfSurveyPositiveResponse.get(key);
			wrapperObj.responseNegativeCount =  mapOfSurveyData.get(key).size() - mapOfSurveyPositiveResponse.get(key);
            listOfInstructorAndCourses.add(wrapperObj);  
        }
        return listOfinstructorAndCourses;
    }


 
This was selected as the best answer
Kenneth KimbrellKenneth Kimbrell

Hello Dushyant Sonwar-

I just want to thank you for all your help. This was a tremendous amount of help, and I hope Salesforce is paying you haha. My final code ended up looking like this in case others may want to see how to acheive something similar:

public class CLS_InstructorMidSurvey{
    
    public list<End_Of_Class_Survey__c> listOfSurveyData {get;set;}
    public list<SurveyWrapper> listOfInstructorAndCourses {get;set;}
    public map<string, list<End_Of_Class_Survey__c>> mapOfSurveyData {get;set;}
    public map<string, integer> mapQ1Yes {get;set;}
    public map<string, integer> mapQ1No {get;set;}
    public map<string, integer> mapIssuesNotNull {get;set;}     
    public list<End_Of_Class_Survey__c> lstSurveyData {get;set;}
    //variables for the instructor issue results method
    public list<End_Of_Class_Survey__c> lstIssueResults {get;set;}
    public string instructorName {get;set;}
    public string courseName {get;set;}
    public integer q1YesCount {get;set;}
    public integer q1NoCount {get;set;}
    public integer issuesNotNull {get;set;}
    
    public class SurveyWrapper{ 
        public End_Of_Class_Survey__c surveyObj {get;set;}
        public integer responseCount {get;set;}
        public integer responseQ1YesCount {get;set;}
        public integer responseQ1NoCount {get;set;}
        public integer responseIssueNotNull {get;set;}
        
        public SurveyWrapper(){
            surveyObj = new End_Of_Class_Survey__c();
            responseCount = 0;
            responseQ1YesCount = 0;
            responseQ1NoCount = 0;
            responseIssueNotNull = 0;
        }
    }
    //Constructor Method
    public CLS_InstructorMidSurvey(){
        instructorName = '';
        courseName = '';
        getListOfInstructorAndCourses();
    }
   //main wrapper class method to getListOfInstructorAndCoursest
    public list<SurveyWrapper> getListOfInstructorAndCourses(){
   listOfSurveyData = new list<End_Of_Class_Survey__c>([SELECT Id, Question_1_Mid_Survey__c, Question_1B_Mid_Survey__c, Question_1A_MidSurvey__c, Instructor__c, Registered_Course__c 
                                                             FROM End_Of_Class_Survey__c 
                                                             WHERE Registered_Course__c != null AND Instructor__c != null AND Question_1_Mid_Survey__c != null
                                                             ORDER BY Instructor__c]);
        listOfInstructorAndCourses = new list<SurveyWrapper>();
        mapOfSurveyData = new map<string, list<End_Of_Class_Survey__c>>();
        mapQ1Yes = new map<string, integer>();
        mapQ1No = new map<string, integer>();   
        mapIssuesNotNull = new map<string, integer>();
        
        for(End_Of_Class_Survey__c e: listOfSurveyData){
            q1YesCount = 0;
            q1NoCount = 0;
            issuesNotNull = 0;   
            lstSurveyData = new list<End_Of_Class_Survey__c>();
            //set a key to find unique string
            string key = e.Instructor__c + '-' + e.Registered_Course__c;
            //get the unique instructor and thier corresponding course
            if(mapOfSurveyData.containsKey(key)){
                lstSurveyData = mapOfSurveyData.get(key);                               
            }
            //get the unique count for Question_1_Mid_Survey__c == yes
            if(mapQ1Yes.containsKey(key)){   
               q1YesCount = mapQ1Yes.get(key);
            }
            if(e.Question_1_Mid_Survey__c == 'Yes'){
                q1YesCount++;
            }
            //get the unique count for Question_1_Mid_Survey__c == no
            if(mapQ1No.containsKey(key)){   
                q1NoCount = mapQ1No.get(key); 
            }
            if(e.Question_1_Mid_Survey__c == 'No'){
             q1NoCount++;
            }
            //get the unique count for Question_1A_MidSurvey__c != null
            if(mapIssuesNotNull.containsKey(key)){
                issuesNotNull = mapIssuesNotNull.get(key);
            }
            if(e.Question_1A_MidSurvey__c != null){
                issuesNotNull++;
            }
            lstSurveyData.add(e);
            mapOfSurveyData.put(key,lstSurveyData); 
            mapQ1Yes.put(key,q1YesCount);
            mapQ1No.put(key,q1NoCount);
            mapIssuesNotNull.put(key,issuesNotNull);        
        }
        //loop through the map's keyset
        for(string key : mapOfSurveyData.keySet()){
            //create a new wrapper object and add the values to the wrapper class
            SurveyWrapper wrapperObj = new SurveyWrapper();
            wrapperObj.surveyObj = mapOfSurveyData.get(key)[0];
            wrapperObj.responseCount = mapOfSurveyData.get(key).size();
            wrapperObj.responseQ1YesCount =  mapQ1Yes.get(key);
            wrapperObj.responseQ1NoCount  =  mapOfSurveyData.get(key).size() - mapQ1Yes.get(key);
            wrapperObj.responseIssueNotNull = mapIssuesNotNull.get(key);
            listOfInstructorAndCourses.add(wrapperObj);  
        }
        return listOfinstructorAndCourses;
    }
    /*Get the Question_1A_MidSurvey__c field text area data for each student that has submitted a survey regarding an instructor and 
    pertaining to a specific course, output the data neatly in a modal for a better user experience*/
    public PageReference getQuestion1BSurveyData(){
        lstIssueResults = new list<End_Of_Class_Survey__c>([SELECT Question_1A_MidSurvey__c, Contact__r.Name
                                                            FROM End_Of_Class_Survey__c 
                                                            WHERE Question_1A_MidSurvey__c != null AND Registered_Course__c != null AND Instructor__c != null AND Instructor__c = :instructorName AND Registered_Course__c = :courseName  
                                                            ORDER BY Instructor__c]);
        system.debug('*********mapQ1Yes: ' + mapQ1Yes);
        system.debug('*********instructorName: ' + instructorName);
        system.debug('*********courseName: ' + courseName);
        system.debug('*********lstIssueResults: ' + lstIssueResults);
        return null; 
    }
}

Cheers!