+ Start a Discussion
Arvind1Arvind1 

Wrapper class problem

Hi All,

 

I have a custom object called questions. There is an object called Answers which has a lookup to Questions object.

 

In a visualforce page datatable, I want to display the questions and next to that all the related answers of that question as radio buttons.

 

But I am not able to identify how do i relate the answers to the questions in my wrapper class. Here is the code I am using

public class SurveyController_new { Invitation__c inv; Invitation__c inv1; List<Question__c> questions = new List<Question__c>(); List<Answers__c> answers = new List<Answers__c>(); Set<id> questionids = new Set<id>(); Map<Id,Answers__c> ans1map = new Map<Id,Answers__c>(); Map<Id,List<Answers__c>> ansmap = new Map<Id,List<Answers__c>>(); Public List<wrpProduct> ProductPricewrp=new List<wrpProduct>(); public SurveyController_new(ApexPages.StandardController controller) { inv=(Invitation__c)controller.getRecord(); } List<String> anss = new List<String>(); public void questionsanswers(){ inv1=[select id,Survey__c from Invitation__c where id=:inv.id]; questions = [select Question_Description__c,Type__c from Question__c where Survey__c=:inv1.Survey__c]; for(Question__c qnids:questions) questionids.add(qnids.id); answers = [select Name,Question__c from Answers__c where Question__c in :questionids]; for(Answers__c anslist:answers) ans1map.put(anslist.Question__c,anslist); system.debug('map1'+ans1map); for(Question__c qnids1:questions){ List<Answers__c> anslist12 = new List<Answers__c>(); system.debug('list'+ans1map.get(qnids1.Id)); anslist12.add(ans1map.get(qnids1.Id)); ansmap.put(qnids1.id,anslist12); } for(Question__c qns:questions) ProductPricewrp.add(new wrpProduct(qns,ansmap.get(qns.Id))); } Public List<wrpProduct> getwrapperProducts(){ return ProductPricewrp; } Public class wrpProduct{ Public Response__c getprdtLine(){ return prdtln; } Response__c prdtln; List<Selectoption> ansoption = new List<Selectoption>(); public List<Selectoption> getansoption(){ return ansoption; } public Question__c questionobj{get;set;} public wrpProduct(Question__c qn,List<Answers__c> Answeroptions){ system.debug('options'+Answeroptions); this.questionobj=qn; prdtln=new Response__c(); for(Integer i=0;i<Answeroptions.size();i++) ansoption.add(new selectoption(Answeroptions[i].Name,Answeroptions[i].Name)); } } }

 When I use this code, I am getting only one answer option next to the question, since the key in map is unique. I can 

do this if I write the answer in the question for loop. Since it may hit the query limit, I want to avoid that. 

Any suggestion on this will be helpful.

 

Thanks

Arvind

bob_buzzardbob_buzzard

I'm guessing that the problem is around this section of code:

 

 

answers = [select Name,Question__c from Answers__c where Question__c in :questionids];
for(Answers__c anslist:answers)
ans1map.put(anslist.Question__c,anslist);

 

Which will overwrite each entry that already exists for a question.  The answer is to change your map to contain a list of answers and add each time, as follows:

 


Map<Id,List<Answers__c>> ans1map = new Map<Id,List<Answers__c>>();

answers = [select Name,Question__c from Answers__c where Question__c in :questionids];

for(Answers__c anslist:answers)
{
List<Answers_c> theList=ans1map.get(anslist.Question__c);
if (null==theList)
{
theList=new List<Answers_c>();
ans1map.put(anslist.Question__c,theList);
}

theList.add(anslist);
}

 

 

 That way you will have a list of answers associated with each product id and can take it from there.

 

 

Message Edited by bob_buzzard on 01-22-2010 06:36 AM
Arvind1Arvind1

Thanks Bob. It worked great.

 

Thanks

Arvind