function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dujsudujsu 

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]);

    }			
	
}

 

kevindotcarkevindotcar

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;

 

    for(iidx=0; iidx < additionalInfoQuestionAvailableResponses.size(); iidx++)
        additionalInfoFieldValueIds.add(additionalInfoQuestionAvailableResponses.Id);

 
...  And then test what type of object it is;

    String aiqar_prefix = Schema.SObjectType.Questions__c.getKeyPrefix();
    
    for(Available_Question_Answer_Options__c aiqar: additionalInfoQuestionAvailableResponses) {
      String aiqar_id = aiqar;
      if(aiqar.startsWith(aiqar_prefix)){
      	System.debug('this is a Questions__c object');
        }
      else { // you should be more vigilant about testing here...
        System.debug('this is a Available_Question_Answer_Options__c object');
        }

 

...But please post if you get other ideas

 

 

 

 

 

dujsudujsu

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)

 

dujsudujsu

Is there a way to just loop through the Lists in Visual Force?  Such as the following?

 

<apex:datalist value="{!additionalInfoQuestionMaps}" var="AIFV">
  |{!AIFV.Question_Text__c}|<br />
     <apex:datalist value="{!additionalInfoQuestionAvailableResponses[AIFV.id]}" var="AIFV_SUB">
         |{!AIFV_SUB.Answer_Text__c}|<br />
     </apex:datalist>
</apex:datalist>