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
swapna muthiyaluswapna muthiyalu 

i have the code

set<string>mEmpId = new set<string>();
map<string,id> MEmpIds = new map<string,id>(); 
list<Contact> con = [SELECT id,eid,AccountId, from Contact where (eid__c IN :mEmpId);
  for(Contact c : con)
        {
             if(mEmpId.contains(m.eid__c) 
            {
                MEmpIds.put(m.eid__c, c.Id);
            }
        }

how to use map of map for map<id,map<string,id>> MEmpIds = new map<string,id>();  and validate the put is from respective account id
jigarshahjigarshah
Swapna,

Can you explain what you are trying to accomplish? Additionally, in a Map<Id, Map<String, Id>> what do the following correspond to?
  • Id
  • String (second map's key)
  • Id (second map's value)
swapna muthiyaluswapna muthiyalu
Hi Jigarshah,
In map<string,id> MEmpIds = new map<string,id>(); i am considering contact and its id, now i want to add the corresponding account id also using
map<id,map<string,id>> MEmpIds.
so when   [MEmpIds.put(m.eid__c, c.Id);] try to put i want to validate that the contact id is from the respective account.
say i have jack(jack account is Burlington textiles) as contact with some eid and when i try to put i have to validate that jack contact accound id is equal to burlington account id.
This iam trying to achieve.
jigarshahjigarshah
Swapna,

Based on your requirement it makes more sense to store it as follows Map<AccountId, (ContactId, ContactRecordInstance)>. This will reduce your iterations and facilitate the type of search you intedn to perform. The sample code to do so is as follows.
 
//acccountContactMap has Key - AccountId, Value - Map<ContactId, Contact>
Map<Id, Map<Id, Contact>> acccountContactMap = new Map<Id, Map<Id, Contact>>();

for(Contact contactRec :[Select Id, Name, AccountId From Contact Limit 10]){
    
    //Ignore the inclusion of Contacts who are not associated to an Account
    if(contactRec.AccountId == null){
        continue;
    }
    
    //Add the Contact record to correponding Account if it already exists
    if(acccountContactMap.containsKey(contactRec.AccountId)){
        
        //Add the Contact to the existing Contacts map
        Map<Id, Contact> contactMap = 
            new Map<Id, Contact>(acccountContactMap.get(contactRec.AccountId));
        contactMap.put(contactRec.Id, contactRec);
        
        //Add Contacts Map to the parent Account map 
        acccountContactMap.put(contactRec.AccountId, contactMap);
    }
    else{
        
        //Create a new entry for that respective Account's Contact records
        acccountContactMap.put(
            contactRec.AccountId, 								
            new Map<Id, Contact>{contactRec.Id => contactRec});
    }
    
    System.debug('****acccountContactMap -> ' + acccountContactMap);
}
swapna muthiyaluswapna muthiyalu
Thank you so much Jigarshah. 

i have one clarification here when i do like this
set<string>mEmpId = new set<string>();
map<id,map<string,id> > MEmpIds = new map<id, map<string,id>>(); 
list<Contact> con = [SELECT id,eid,AccountId, from Contact where (eid__c IN :mEmpId);
  for(Contact c : con)
        {
             if(mEmpId.contains(c.eid__c) 
            {
                mEmpIds.put(c,accountid).put(c.eid__c, c.Id); 
0r can i write like this
                mEmpIds.put(c,accountid,c.eid__c, c.Id);  
or
      mEmpIds.get(c,accountid).put(c.eid__c, c.Id);  
            }
        }

will this work or i need to loop first map<string,id> and add to the outer map as u mentioned.
Please advice.

Swapna
swapna muthiyaluswapna muthiyalu
please explain how i can i test this piece of code is working in developer console.
jigarshahjigarshah
You can use the code below.
set<string>mEmpId = new set<string>();
map<id,map<string,id>> MEmpIds = new map<id, map<string,id>>(); 

for(Contact c : [SELECT id,eid,AccountId, from Contact where (eid__c IN :mEmpId)]){

	if(mEmpId.contains(c.eid__c) {

		mEmpIds.get(c.AccountId).put(c.eid__c, c.Id);
        System.debug('***Key => ' + c.AccountId + ' Value => ' + mEmpIds.get(c.AccountId).values());
	}
}//for

In order to verify this, paste this code in Developer Console > Debug > Execute Anonymous Window and click Execute. Check the DEBUG checkbox in the log window to filter only the System.debug() statements from the entire log and check the values that are printed.