You need to sign in to do that
Don't have an account?
Kenneth Kimbrell
How to show only one record that contains the same field values in an APEX SOQL Query
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. Here is an example:
As you can see Bobbie teaches a class in Developmental Psychology and also in Anatomy and Physiology -- he has also received a survey review from a student in each.
Bruce Priddy teaches Developmental Psychology as well.
Christy teaches Developmental Psychology too and has received 3 survey reviews and all 3 records are visible. I only want to output one record not multiple records that have the same instructor name and registered course. The reason why is for report user ability. I want to then add into a wrapper class the total response count for a specific instructor and and specific course.
I tried using a set, map, wrapper, etc... But the issue is instructor's names will be used more than once and so will the courses. So a set would not help. Unless I create a list, add the set to the new list, then make conditional statements and add to the list only when conditions are met. That way seems promising but I have not been able to completely make it work. Mostly because there is a time when both the instuctor and the course are in the set so I can no longer make a condition. I am convinced there has to be an easy way to just only output a record once if there are specified field names the same. I just can't seem to find a way. So I am reaching out to my community. Here is the method I am using:
//Instrustor //Registered Course Becky Gosky English Composition Bobbie Strother Developmental Psychology Bobbie Strother Anatomy & Physiology Bruce Priddy Developmental Psychology Christy Saladino Developmental Psychology Christy Saladino Developmental Psychology Christy Saladino Developmental Psychology Christy Saladino NCLEX Preparation
As you can see Bobbie teaches a class in Developmental Psychology and also in Anatomy and Physiology -- he has also received a survey review from a student in each.
Bruce Priddy teaches Developmental Psychology as well.
Christy teaches Developmental Psychology too and has received 3 survey reviews and all 3 records are visible. I only want to output one record not multiple records that have the same instructor name and registered course. The reason why is for report user ability. I want to then add into a wrapper class the total response count for a specific instructor and and specific course.
I tried using a set, map, wrapper, etc... But the issue is instructor's names will be used more than once and so will the courses. So a set would not help. Unless I create a list, add the set to the new list, then make conditional statements and add to the list only when conditions are met. That way seems promising but I have not been able to completely make it work. Mostly because there is a time when both the instuctor and the course are in the set so I can no longer make a condition. I am convinced there has to be an easy way to just only output a record once if there are specified field names the same. I just can't seem to find a way. So I am reaching out to my community. Here is the method I am using:
public list<wrapper> getlistWrap(){ wrap = new list<wrapper>(); listSurveyData = 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]); listInstructors = new list<Instructor__c>([SELECT Id, Name FROM Instructor__c]); getClassName = new set<string>(); getInstructorName = new set<string>(); count = 0; for(integer i = 0; listSurveyData.size() > i; i++){ if(listSurveyData[i].Instructor__c != '' && listSurveyData[i].Instructor__c != null){ getInstructorName.add(listSurveyData[i].Instructor__c); } if(listSurveyData[i].Registered_Course__c != '' && listSurveyData[i].Registered_Course__c != null){ getClassName.add(listSurveyData[i].Registered_Course__c); } } listClassName = new list<string>(getClassName); instructorName = new list<string>(getInstructorName); for(count = 0; listClassName.size() > count; count++ ){ wrap.add(new wrapper(instructorName[count], listClassName[count])); } return wrap; }
If you want output like below , then wrapper class is the best option in my opinion.
//Instrustor //Registered //Responses
Course Becky Gosky English Composition 1
Bobbie Strother Developmental Psychology 1
Bobbie Strother Anatomy & Physiology 1
Bruce Priddy Developmental Psychology 1
Christy Saladino Developmental Psychology 3
Christy Saladino NCLEX Preparation 1
1) Create a Wrapper Class
2) Fill the wrapper with survey data Below is the code
on vf page :
All Answers
The description you have given above , from what i understood.
Currently your output is this and you want is this
Then you can use set/map to create a unique key to filter something like below:
Hope this helps :)
This helped, exactly what I was looking for. What do you think would be the best way now to get the number of responses that each teacher recieved from the course? For example Christy received 3 survey responses from Christy Saladino Developmental Psychology. Now in that same row I want to be able to show an output for the response count per each teacher in the course. In this case the output on that row would be:
//Instrustor //Registered //Responses
Course Becky Gosky English Composition 1
Bobbie Strother Developmental Psychology 1
Bobbie Strother Anatomy & Physiology 1
Bruce Priddy Developmental Psychology 1
Christy Saladino Developmental Psychology 3
Christy Saladino NCLEX Preparation 1
If you want output like below , then wrapper class is the best option in my opinion.
//Instrustor //Registered //Responses
Course Becky Gosky English Composition 1
Bobbie Strother Developmental Psychology 1
Bobbie Strother Anatomy & Physiology 1
Bruce Priddy Developmental Psychology 1
Christy Saladino Developmental Psychology 3
Christy Saladino NCLEX Preparation 1
1) Create a Wrapper Class
2) Fill the wrapper with survey data Below is the code
on vf page :
So this is what I did, (I had to make modifications becuase it threw errors from what you had) but I can not seem to gain access to the Instructor__c and Registered_Course__c fields inside of the wrapper class. I get an error:
Unknown property 'CLS_InstructorMidSurvey.SurveyWrapper.instructor__c
Please do the following changes:
Change line no 39 statement
to
and
line no 6
to
use below code to display data on your vf page.
This should work fine.
If still issue continues . Could you post your vf page code , how you are using the variables to displaying the data on vf page?
That was it man! I knew something was up. Because I originally had to change the wrapper surveyObj variable dataType into a list. Because the other code seemed to be getting the values of the list as a string and not the actual survey object itself. Thanks man! A lot of people can learn from this thread. A very clever way of accomplishing what I was after.