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
prasanth kumarprasanth kumar 

invalid relation ship please help

i am trying to execute the code in developer console, but i am getting this error. please some one help me. 

Invalid foreign key relationship: Account.contact


 
map<string,list<string>> m2=new map<string,list<string>>();

for(account a1:[select id,name,(select id,lastname,phone from contacts cons) from account])
{
    m2.put(a1.name,a1.contact.phone);
}

 
bob_buzzardbob_buzzard
When you select the contacts from an account you get a list of them, as they are on the child side of the relationship. Also, you are trying to put a single string as the value of a map defined as key/list of strings pairs, so that would fail too.

To iterate the accounts/contacts and generate a list of phone numbers against account name you'd need something like:
 
map<string,list<string>> m2=new map<string,list<string>>();

for(account a1:[select id,name,(select id,lastname,phone from contacts cons) from account])
{
    LIst<String> phones=new List<String>();
    m2.put(a1.name, phones);
    for (Contact con : a1.cons)
    {
       phones.add(con.Phone);
    }
}