You need to sign in to do that
Don't have an account?
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 :)
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. 2) Calculate the postive and negative response for the survey.
All Answers
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. 2) Calculate the postive and negative response for the survey.
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:
Cheers!