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
Dumber_Texan2Dumber_Texan2 

SOQL statement for retrieveing Contact information where Account Name = 'ABC Company'

I've tried this query several ways and I can't get it to work unless I use a number for the Accountid. I would like to use a name instead of a number. According to the documentaiotn on Contact filelds found here (http://na24.salesforce.com/help/pdfs/en/salesforce_field_names_reference.pdf) (page 30), Account Name is an ID instead of a name. Anyway, this is confusing.

Here is the query that works.

Select Id, FirstName, LastName, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, MobilePhone, Email, PhotoUrl, CreatedDate, Accountid from Contact where Accountid = 'SOMEID'

Here is how I would like to filter the results.

I would like to retreive all Contacts where the Account Name = "ABC Company'.

Thanks
Best Answer chosen by Dumber_Texan2
Himanshu ParasharHimanshu Parashar
Hi Brian,

Yes that is correct. AccountId is reference field on Contact so Accountid holds Account record id, so in your case if you want to filter out results on the basis of Account Name you need to write following SOQL
 
select id from contact where Account.Name='ABC Company'

makes sense?


Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar
Hi Brian,

Yes that is correct. AccountId is reference field on Contact so Accountid holds Account record id, so in your case if you want to filter out results on the basis of Account Name you need to write following SOQL
 
select id from contact where Account.Name='ABC Company'

makes sense?


Thanks,
Himanshu
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below query :-

Select Id, FirstName, LastName, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, MobilePhone, Email, PhotoUrl, CreatedDate, Accountid from Contact where Account.Name='ABC Company'


I hope this will help you. Please let us know if this will help you.
Thanks
Amit Chaudhary
Dumber_Texan2Dumber_Texan2
That worked! Thanks for the help. I wonder why the field is not clearly defined in the documentation. Is it in error?
Amit Chaudhary 8Amit Chaudhary 8
This is not error. You can check parent field by like that. 
In Standard object you just need to access field by . like Account.Name etc

in custom object object you need to access field by __r. 

"__r" is used for retrieving field values from the object's related another object when those objects have relationship via Lookup field.
See links below for detail and samples
In Formula:
http://developer.force.com/cookbook/recipe/displaying-fields-from-a-related-record-on-a-detail-page

In SOQL:
http://www.forcetree.com/2009/07/understanding-relationships-in.html

Please let  us know if this will help you

 
Himanshu ParasharHimanshu Parashar
Hi Brian,

There is no error in documentation, if you will check again that documentation then you will find that Account Name is defiend as Label, so anytime you will see contact detail page instead of showing Account id SFDC shows referred account name for user convenience which makes sense.

see following screenshot

User-added image

Now talk about the AccountID, Contact and Account share relationship with each other that is why Contact Object contain ID of Account record (foreign key in SQL).

In SFDC whenever you need to get any data from any relational object you query that using relationship name . field name, in your case we did that as Account.Name where Account is relationship name and Name is field of that object.

Makes sense ?