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
LaurenP6777LaurenP6777 

Apex Controller: Combine two lists


 

Hello, 

I am trying to create a visualforce page that displays all of the current User's Opportunities. I would like to include Opportunities that they own AND all Opportunities where they are a member of the OpportunityTEam. 

My first attempt was to do a query like this:

MyOpTeam =[SELECT id, Record_Type_Name__c,  Name, StageName, Amount,  Owner.Id FROM Opportunity WHERE id IN (SELECT OpportunityId FROM OpportunityTeamMember WHERE (UserId=:UserInfo.getUserId() )OR (Opportunity.OwnerId=:UserInfo.getUserId())];  
*** PROBLEM: this only pulled opportunities that had team members. Any opportunities that I owned that did not have a team member record were excluded. 

Could anyone look at my current code and tell me how I can go about getting these two SEPARATE queries into one list? 
public class Oppsownerteam {
public Oppsownerteam(ApexPages.StandardController controller) {
        
}
    
 Id Mouse = Userinfo.getUserId();
 public List<Opportunity> MyOwnedOpps;
 public List<Opportunity> MyTEamOpps;
 

            Public Integer Get MyOwnedOpps()
               {
                  MyOwnedOpps =[SELECT id, Record_Type_Name__c,  Name, StageName, Amount,  Owner.Id FROM Opportunity WHERE ownerid =:Mouse];  
                return   MyOwnedOpps;       
                }
            

             Public List<Opportunity> GetMyTEamOpps()
               {                        
                MyTEamOpps =[SELECT id, Record_Type_Name__c,  Name, StageName, Amount,  Owner.Id FROM Opportunity WHERE id IN (SELECT OpportunityId FROM OpportunityTeamMember WHERE UserId=:Mouse)];  
                   
                return MyTEamOpps;                               
                 }
            
  



}

 
Best Answer chosen by LaurenP6777
CyberJusCyberJus
You want to use the addAll method to add one list into another.
 
List<Opportunity> allOpportunities = new List<Opportunity>();
allOpportunities.addAll(MyOwnedOpps);
allOpportunities.addAll(GetMyTeamOpps);

You also want to be careful in your initial queries that you dont pull some of the same opportunities in each list, then it would show up twice. 

All Answers

Alex KirbyAlex Kirby
Hi Lauren,

Could you add another list then aadd both lists to that or add the second list into the first?

Option 1: 
 
public List<AggregateResult> AllOpps;

AllOpps.Add(MyOwnedOpps);

AllOpps.Add(GetMyTeamOpps);

or

Option 2:
 
MyOwnedOpps.add(GetMyTeamOpps);

 
CyberJusCyberJus
You want to use the addAll method to add one list into another.
 
List<Opportunity> allOpportunities = new List<Opportunity>();
allOpportunities.addAll(MyOwnedOpps);
allOpportunities.addAll(GetMyTeamOpps);

You also want to be careful in your initial queries that you dont pull some of the same opportunities in each list, then it would show up twice. 
This was selected as the best answer