+ Start a Discussion
minciminci 

Strange compilation error

I wrote this code :

 

List<Contact> contactsToCount = [Select Id, Account from Contact where Account in : account_id_list];

Tkan i got an compilation error saying that there is not sych a field

Account in sObject contact.

Contact is being given by salesforce ans so the field Account (Label : Account Name).

Can someone tell me what is the p[roblem?

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Account is the relationship name between contact and account, and thus you can't select it.

 

You can get the account id, using the following:

 

 

List<Contact> contactsToCount = [Select Id, AccountId from Contact where Account in : account_id_list];

 

 

or the account name using the following:

 

 

List<Contact> contactsToCount = [Select Id, Account.Name from Contact where Account in : account_id_list];

 

 

 

 

 

 

All Answers

bob_buzzardbob_buzzard

Account is the relationship name between contact and account, and thus you can't select it.

 

You can get the account id, using the following:

 

 

List<Contact> contactsToCount = [Select Id, AccountId from Contact where Account in : account_id_list];

 

 

or the account name using the following:

 

 

List<Contact> contactsToCount = [Select Id, Account.Name from Contact where Account in : account_id_list];

 

 

 

 

 

 

This was selected as the best answer
minciminci

Thank you!!! :-) Solved!