You need to sign in to do that
Don't have an account?
How to merge data from two lists in Apex or VisualForce (Salesforce)?
What is the best way to merge the two Lists below in Apex or VisualForce? The data is related, but cannot be queried together, so I need to combine the two Lists.
Things to note:
1. The two lists: the additionalInfoQuestionMaps is the parent List and the additionalInfoQuestionAvailableResponses is the child. There can be 1 to many children.
2. I need to join the Lists on the Question_Type_Info__c object.
Basically its a set of Questions coming from the Parent List, then I need to associate possible answers to those questions such as Yes and No that reside in the Child List. Those results will be displayed on a VF page in a form.
public with sharing class mytest { public List<Questions__c> additionalInfoQuestionMaps {get;set;} public List<Available_Question_Answer_Options__c> additionalInfoQuestionAvailableResponses {get;set;} public List<retrieveMergedQuestionList> retrieveMergedQuestionLists {get;set;} public String buildId {get;set;} public Build__c build {get;set;} public mytest() { buildId = 'a1DV00000001BBBBBB'; build = sharedfile.getBuild(buildId); updateAdditionalInfoQuestionMaps(); updateAdditionalInfoFieldValueIds(); updateAdditionalInfoQuestionAvailableResponses(); } public void updateAdditionalInfoQuestionMaps() { additionalInfoQuestionMaps = new List<Questions__c>([SELECT Id, Name, Associated_Product_Item__c, Associated_Product__c, Label__c, (select Response__c from Responses_Object__r), Question_Type_Info__c, Required__c FROM Questions__c WHERE Associated_Product__c = :build.Associated_Product__c ORDER BY Sort_Index__c, Label__c]); } public Set<Id> additionalInfoFieldValueIds = new Set<Id>(); public void updateAdditionalInfoFieldValueIds(){ for (Questions__c aimb : additionalInfoQuestionMaps) { additionalInfoFieldValueIds.add(aimb.Question_Type_Info__c); } } public void updateAdditionalInfoQuestionAvailableResponses() { additionalInfoQuestionAvailableResponses = new List<Available_Question_Answer_Options__c>([select Id, Field_Value__c, Question_Type_Info__c from Available_Question_Answer_Options__c where Question_Type_Info__c IN :additionalInfoFieldValueIds]); } }
Hi jujsu,
Just tasking a stab at it- in effect your "additionalInfoFieldValueIds" list is polymorphic;
I'd just add the other objects to the list;
... And then test what type of object it is;
...But please post if you get other ideas
Thanks for the reply- egads! I am new to the polymorphism concept..so here's a shot at it. Im not completely sure what I need to do with the IF statement. I see it will tell me which object is which (still trying to get a grasp on this as well) and that is great. Seems like the results are still separate though... This may help both of us. Here's what I need to display on the screen...sometimes it helps to see the end result (at least for me). Just want to make sure I am explaning properly too.
Question 1 (from Questions__c Object)
- Yes (from Available_Question_Answer_Options__c Object)
- No (from Available_Question_Answer_Options__c Object)
Question 2 (from Questions__c Object)
- Yes (from Available_Question_Answer_Options__c Object)
- No (from Available_Question_Answer_Options__c Object)
- Maybe (from Available_Question_Answer_Options__c Object)
Is there a way to just loop through the Lists in Visual Force? Such as the following?