+ Start a Discussion
Vijay@sfdcVijay@sfdc 

Map issue

Hello All,
map<id, account> am= new map<id,account>([select id,name from account limit 10 ]);
system.debug('ammm===>'+am)
 i have executed this code script , its working fine , but i tryied to executed bellow code i am getting error , please help me
what is the issue
map<name, account> am= new map<name,account>([select id,name from account limit 10 ]);
system.debug('ammm===>'+am);

Kindly help me with my doubt

Thanks :)
Best Answer chosen by Vijay@sfdc
3 Creeks3 Creeks
You can only put the Id into the map directly from a query.  Also, "Name" is not a variable type so you would use String instead.

If you want to use the account name as the key of your map, you will have to build the map in a loop:
 
Map<String, Account> am = new Map<String, Account>();
List<Account> act = [Select id, name from Account limit 10];

for (Account a : act) { 
   am.put(a.Id, a);
}

 

All Answers

3 Creeks3 Creeks
You can only put the Id into the map directly from a query.  Also, "Name" is not a variable type so you would use String instead.

If you want to use the account name as the key of your map, you will have to build the map in a loop:
 
Map<String, Account> am = new Map<String, Account>();
List<Account> act = [Select id, name from Account limit 10];

for (Account a : act) { 
   am.put(a.Id, a);
}

 
This was selected as the best answer
Vikash Kumar JhaVikash Kumar Jha

name is not datatype, by default id will come up as a key because that is unique.
 
Vijay@sfdcVijay@sfdc
Thanks Guys , your work is Appreciable