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
dharmik thummardharmik thummar 

I have map<String,List<Contact>> in which i've stored account Id as A string and it's related contact. now I want another map which can store map<AccountName,map<AccounID,List<Contact>>>. how to do that?

Best Answer chosen by dharmik thummar
Sandeep YadavSandeep Yadav
Then you should use another Map which contains account name as a value.
try this ..
Map<String,List<Contact>> firstMap = new Map<String,List<Contact>>();
Map<string,string> anotherMap = new  Map<string,string>();
List<Account> acList = [select id,name,(select id,lastname from contacts) from account ];
for(Account ac :  acList){
	if(ac.contacts.size() != null){
		firstMap.put(ac.Id,ac.contacts);
		anotherMap.put(ac.Id,ac.Name);
	}
}
system.debug('<<firstMap is :>>'+ firstMap);

Map<string,Map<String,List<Contact>>> secondMap = new Map<string,Map<String,List<Contact>>>();
for(Account ac : acList){
	secondMap.put(anotherMap.get(ac.Id).Name,firstMap);
}
system.debug('<<Second Map is : >>'+secondMap);

Thanks
Sandeep

All Answers

Sandeep YadavSandeep Yadav
Hi Dharmik,
As per my Understanding i have done something which meet criteria that u define.
Map<String,List<Contact>> firstMap = new Map<String,List<Contact>>();
List<Account> acList = [select id,name,(select id,lastname from contacts) from account ];
for(Account ac :  acList){
	firstMap.put(ac.Id,ac.contacts);
}
system.debug('<<firstMap is :>>'+ firstMap);

Map<string,Map<String,List<Contact>>> secondMap = new Map<string,Map<String,List<Contact>>>();
for(Account ac : acList){
	secondMap.put(ac.name,firstMap);
}
system.debug('<<Second Map is : >>'+secondMap);

Let me know it is helpful or not for you.

Thanks
Sandeep
dharmik thummardharmik thummar
List<Account> lstAccount = [SELECT Id,Name,(SELECT Id, Name FROM Contacts) FROM Account];
        
        for(Account acc : lstAccount)
        {
            this.accountContactMap.put(acc.id, acc.Contacts);
        }

We are doing same Sanndeep, but I want account name of every Id which is stored in your firstMap map. 
Sandeep YadavSandeep Yadav
Then you should use another Map which contains account name as a value.
try this ..
Map<String,List<Contact>> firstMap = new Map<String,List<Contact>>();
Map<string,string> anotherMap = new  Map<string,string>();
List<Account> acList = [select id,name,(select id,lastname from contacts) from account ];
for(Account ac :  acList){
	if(ac.contacts.size() != null){
		firstMap.put(ac.Id,ac.contacts);
		anotherMap.put(ac.Id,ac.Name);
	}
}
system.debug('<<firstMap is :>>'+ firstMap);

Map<string,Map<String,List<Contact>>> secondMap = new Map<string,Map<String,List<Contact>>>();
for(Account ac : acList){
	secondMap.put(anotherMap.get(ac.Id).Name,firstMap);
}
system.debug('<<Second Map is : >>'+secondMap);

Thanks
Sandeep
This was selected as the best answer