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
MJRMJR 

Error: Compile Error: Illegal assignment from String to SOBJECT:Account at line 37 column 9

my code is

Map<Id,Account> newmp = new map<Id,Account>([Select Id,Name from Account limit 10]);
for(Account acc : newmp.values())
{
string codesId = newmp.get('United Oil & Gas, UK');

system.assertEquals(codesId,'0019000000PCwb9AAD');
system.debug('account:'+codesId);
}

Sfd developerSfd developer

HI,

 

try this,

 

string codesId = newmp.get(acc.Id).Id;

system.assertEquals(codesId,'0019000000PCwb9AAD');

MJRMJR

not working

Avidev9Avidev9

Well doing it all wrong!
First of all this

Map<Id,Account> newmp = new map<Id,Account>([Select Id,Name from Account limit 10]);

Code creates a map of Id and Account.
So Map has
Id(not the name) as Key
Sobject Account as Value

 

 

So you have to actually do something like this

 

Map<String,Id> newmp = new Map<String,Id>();

for(Account acc : [SELECT Id,Name FROM Account LIMIT 10]){

newmp.put(acc.Name,acc.Id);

}

 


PS : From direct query you can only generate map of Id and Sobject. For anything other then this you have to iterate through the records to generate the map