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
snippets@sfsnippets@sf 

SOQL Query

Whats the best way to get 

 

Map<String,id> accountNameVsId = [Select id, Accountname form account];

 

Please suggest

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_LearnerSFDC_Learner

map<id,Account> mapAccs = new map<id,Account>([select id,name from Account]);

 

Now Each account is mapped to Id of that account.

 

 

 

 

All Answers

Jeff MayJeff May

there maybe a more elegant way but I would do:

 

Map<String,id> accountNameVsId = new Map<String,id>();

for (Account a : [Select id, Accountname form account]) {
    accountNameVsId.put(a.Accountname, a.id);
}

 

SFDC_LearnerSFDC_Learner

map<id,Account> mapAccs = new map<id,Account>([select id,name from Account]);

 

Now Each account is mapped to Id of that account.

 

 

 

 

This was selected as the best answer
snippets@sfsnippets@sf

Thanks,

For the quick solution