0

I'm trying to use apex/visualforce to surface all the questions and replies to these questions. I'm having a hard time wrapping my head around how to go about doing this. I'm able to display one question and I can pass that question ID to query the Replies table, but how would I make a list of questions that have a list of associated replies? I want it listed out kind of like this on the page:

Question 1

  • reply 1
  • reply 2
  • reply 3

Question 2

  • best answer (reply 2)
  • reply 1
  • reply 3
  • reply 4

Thanks, Sean

UPDATE: Here's the code that I'm using to get the colletion of questions with their associated replies. For some odd reason, I'm getting the correct replies, but null for the question.

public List<WrapperQuestions> wrapperQuestions                             { get; set; }
public List<Question> questionsList                                { get; set; }

public List<Question> getQuestions() {
        if(questionsList == null) {
            questionsList = new List<Question>();
            for(Question q: [SELECT q.Id, q.CreatorSmallPhotoUrl, q.CreatorName, q.CreatedDate, q.CreatedById, q.Body FROM Question q]) {
                // As each contact is processed we create a new WrapperQuestions object and add it to the contactList
                questionsList.add(q);
            }
        }
        return questionsList;
    }

    public class WrapperQuestions   // wrapper class to handle multiple objects.
    {
        public List<Question> questions              { get; set; }
        public List<Reply> replies            { get; set; }

        public WrapperQuestions(List<Question> question)
        {
             question = questions;
             //replies = replies;
             replies = [SELECT CreatedDate, CreatedById, QuestionId, Body, UpVotes, DownVotes FROM Reply];
        }
    }
2
  • What exactly is your question? It's pretty unclear what you're asking. Are you talking about displaying them on a VF page - if yes, create collections to hold the questions and replies in the controller, and use apex:repeat or another iterating component to write them to the VF page Aug 13, 2015 at 18:09
  • Thanks Brian. Yes, my intent is to display the Questions/Replies on a VF page. I know how to do it using one question with multiple replies, but I'm having trouble doing it for multiple questions with each having multiple replies. Is there any example you can show me of achieving this (or even linking to a resource online)?
    – slim
    Aug 13, 2015 at 18:19

1 Answer 1

2

You haven't specified details about your implementation (how you're sourcing the posts in your controller, what you want to display from the replies, etc etc), but to display multiple replies for multiple questions, you can use nested iteration components.

Think of it as just a nested for loop - your outside apex:repeat is iterating over your list of questions, and for each question, your inner apex:repeat iterates over your list of replies for that question.

At an abstract level, it will look like this:

<apex:repeat value="{!CollectionThatHoldsQuestions}" var="currentQuestion">
    <apex:outputPanel>
        {!currentQuestion}
    </apex:outputPanel>
    <apex:repeat value="{!InnerCollectionThatHoldRepliesPerQuestion}" var="currentReply">
        <apex:outputPanel>
            {!currentReply}
        </apex:outputPanel>
    </apex:repeat>
</apex:repeat>

This example displays how to nest apex:repeat to display posts and replies.

4
  • would you recommend to do a WrapperClass that stores a list of questions and a list of answers? Or would you keep them both separate?
    – slim
    Aug 13, 2015 at 19:58
  • You should definitely use a wrapper class in your controller yeah, with nested collections. You need to have some way to associate the replies with the questions. Aug 13, 2015 at 20:05
  • I updated my question to add the code I'm using to get the collections. For some reason (I tested this using execute anonymous), I'm getting the replies, but a null value for the question.
    – slim
    Aug 13, 2015 at 20:11
  • Your original question was answered, if you have an additional or follow-up question you should post a new question to help ensure that posts in this community remain one, specific question. Aug 13, 2015 at 20:15

You must log in to answer this question.

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