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
Sascha DeinertSascha Deinert 

Tow maps merge into one list

Hi,

I have two maps and I want to merge this maps into one list, key should be the account. My goal is an overview of the accounts with the opportunities.
How can I merge these lists?
 
public map<Id, Account> getMapFieldLabel1() {
    Map<Id, Account> mapField_Label1= new Map<Id, Account>([SELECT Id, Name FROM Account LIMIT 10]);
    return mapField_Label1;
}

public map<Id, Opportunity> getMapFieldLabel2() {
    Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
    return mapField_Label2;
}
<apex:pageBlockSection columns="2" collapsible="false">              
            <apex:repeat value="{!MapFieldLabel1}" var="item" >
                  <apex:outputText value="{!MapFieldLabel1[item].Id}" />
                   <apex:outputText value="{!MapFieldLabel1[item].Name}" />
            </apex:repeat>   
            </apex:pageBlockSection>
 </apex:pageblock>
 <br/><br/>
 <apex:pageblock>
  <apex:pageBlockSection columns="3" collapsible="false">              
            <apex:repeat value="{!MapFieldLabel2}" var="item" >
                  <apex:outputText value="{!MapFieldLabel2[item].Account.Id}" />
                  <apex:outputText value="{!MapFieldLabel2[item].Id}" />
                   <apex:outputText value="{!MapFieldLabel2[item].Name}" />
            </apex:repeat>   
            </apex:pageBlockSection>
 </apex:pageblock>
Thanks,
Sascha

 
Raj VakatiRaj Vakati
try like this 
 
public map<Id, Account> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
	 Opportunity opps =  mapField_Label2.get(ids);
	 finalMap.put(opps.Account , opps);
  }
   
}

 
Sascha DeinertSascha Deinert
Hi Raj,

I get an error:

Error: Compile Error: Method does not exist or incorrect signature: void put(Account, Opportunity) from the type Map<Id,SObject>

Do you know what happen?

Thanks,
Sascha
Raj VakatiRaj Vakati
LIke below pls
 
public map<Id, Account> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
	 Opportunity opps =  mapField_Label2.get(ids);
	 finalMap.put(opps.AccountId , opps);
  }
   
}

 
Sascha DeinertSascha Deinert
Now I get this message

Compile Error: Missing return statement required return type: Map<Id,Account>

If I add an RETURN STATEMENT like below:
 
public map<Id, Account> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
     Opportunity opps =  mapField_Label2.get(ids);
     finalMap.put(opps.AccountId , opps);
  }
  
 return finalMap;
   
}
If get an error:
Compile Error: Illegal conversion from Map<Id,SObject> to Map<Id,Account>

Do you know how can I solve this issue?

Thanks,
Sascha
Raj VakatiRaj Vakati
public map<Id, Sobject> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
	 Opportunity opps =  mapField_Label2.get(ids);
	 finalMap.put(opps.Account , opps);
  }
   
}

Use above
Sascha DeinertSascha Deinert
But now I get the error like 3 posts before.

Method does not exist or incorrect signature: void put(Account, Opportunity) from the type Map<Id,SObject>
Raj VakatiRaj Vakati
My bad and was doing multitasking .. this correct and final
 
public map<Id, Sobject> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
	 Opportunity opps =  mapField_Label2.get(ids);
	 finalMap.put(opps.AccountId, opps);
  }
   
}

 
Sascha DeinertSascha Deinert
ok, but now is the return statement missing. 
which variable should be return?

if I return the finalmap I get the error:
Compile Error: Illegal conversion from Map<Id,SObject> to Map<Id,Account>
Raj VakatiRaj Vakati
use this and ping me if you have any issue ..
 
public map<Id, Sobject> getMege() {
      Map<Id, Sobject> finalMap= new Map<Id, Sobject>([SELECT Id, Name FROM Account LIMIT 10]);
   Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
  
  for(Id ids :mapField_Label2.keySet()){
	 Opportunity opps =  mapField_Label2.get(ids);
	 finalMap.put(opps.AccountId, opps);
  }
  return finalMap;
   
}

 
Raj VakatiRaj Vakati
rajamohanvakati@gmail.com
Sascha DeinertSascha Deinert
I can't reach via Mail.

How can I return the Account Information?
<apex:pageblock >
  <apex:pageBlockSection columns="2" collapsible="false">              
            <apex:repeat value="{!Mege}" var="item" >
                  <apex:outputText value="{!Mege[item].AccountId}" />
                   <apex:outputText value="{!Mege[item].Name}" />
            </apex:repeat>   
            </apex:pageBlockSection>
</apex:pageblock>
Error:
Invalid field AccountId for SObject Account
Error is in expression '{!Mege[item].AccountId}' in component <apex:outputText> in page map1

Any ideas?
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Sascha Deinert,
Kindly find solution.
 
public class TestApex{
    public static List<sObject> mergeTwoMap(){
        Map<Id, Account> mapField_Label1= new Map<Id, Account>([SELECT Id, Name FROM Account LIMIT 10]);
        Map<Id, Opportunity> mapField_Label2= new Map<Id, Opportunity>([SELECT Id, Account.Id, Name FROM Opportunity]);
        List<sObject> objectList = new List<sObject>();
        objectList.addAll(mapField_Label1.values());
        objectList.addAll(mapField_Label2.values());
        return objectList;
        
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.