Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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

    }           

}

1 Answer

0 votes
by (32.1k points)

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

    }

 Looking for a comprehensive Salesforce Training? Enroll now!

Browse Categories

...