2

I have two custom objects, Question__c and Response__c. There is a lookup field named Related_Question__c on the Response__c object to relate the two. In my VF controller I have two Lists, one for each object. My objective is to find all records in the Question List that do not have a match in the Response List (ie questions that haven't been answered yet).

I believe I need to use a map for this but am unsure of exactly how to accomplish this. Any advice would be appreciated. Thanks in advance.

Here's what I have so far to build the two Lists:

List <Question__c> questions = [select Id, Name From Question__c];
List <Response__c> questionResponses = [select Id, Name, Related_Question__c From Response__c Where Related_Question__c IN: questions};

3 Answers 3

3

Can you try something like this ?

List<Id> l = new List<Id>();
Map<Id,Response__c> qr = new Map<Id,Response__c>();
for(Response__c r : questionResponses)
{

    qr.put(r.Related_Question__r.Id,r);
}
for(Question__c q :  questions)
{

  if(qr.get(q.Id) == null)
 {
      l.add(q.Id);    
}

}
System.debug('~~~ Non matching Ids '+ l);
3
  • 1
    Yay someone managed it without SOQL.
    – Adrian Larson
    Sep 22, 2015 at 0:06
  • @AdrianLarson The answer still has 2 soql queries, questionResponses and Questions are lists which use 2 diff soql
    – Rao
    Sep 22, 2015 at 5:14
  • @ZenSeeker I had to modify your code slightly (needed to change what was being put into the map and use add, instead of put, for getting the question record into the list) but it got me headed down the right path. Thanks much.
    – John_Henry
    Sep 22, 2015 at 13:28
1

Try having one SOQL and check if the list of response for that question is empty like below (*Please add a where clause or limit your query)

List<questions__c> questions = [select id,Name,(select Id, Name, Related_Question__c From Responses__r) from question__c];
    for (question__c que: questions) {
        if (que.responses__r.isempty()) {
            //Response is missing for this question.
        } else {
            //From the list of responses for every question in your list do further updates as required.
            for (reponse__c response: que.reponses__r) {
                //do whatever with the response.      
            }
        }
    }  
0

This will return Questions__c that have no associated with Responses:

   List<Question__c> questionsNoResponse = [select Id, Name from Questions__c where Id not in (select Related_Questions__c from Response__c)]

This will return Questions__c with a list of associated responses. The list size for the associated responses will be 0 if there are no records:

  List<Question__c> questionsAndResponse = [select Id, Name from Questions__c, (select Id, Name from Questions__r) from Questions__c)]

The first SOQL will give you the list you are looking for without writing apex to loop through your original lists. If you use the 2nd query then you can loop like this:

  List<Question__c> quesWithNoResponse = new List<Questions__c>();
  List<Question__c> qList = List<Question__c> questionsAndResponse = [select Id, Name from Questions__c, (select Id, Name from Questions__r) from Questions__c)];

  for (Question__c item: qList){
     if (item.Questions__r.size() = 0){
        quesWithNoResponse.add(item);
     }
  }

NOTE: in the examples above I have assumed that your child relationship API name is Questions__r. You should check the configuration page for the child relationship name for the field Related_Question__c.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .