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
Robert RobinsonRobert Robinson 

SOQL Query: Extracting a name for a custom field

I have a custom field (Tenant__c) which represents a lookup value for Accounts. I am trying to write a SOQL query to extract the names in this field, rather than the 18 character case safe value. I know when you are trying to to pull the name for the Account field, you express it as "Account.Name", but I cannot get the correct syntax for this custom field (Tenant.Name__c does not work). What is the correct syntax in this situation? Thank you.
Best Answer chosen by Robert Robinson
Akash_CRMAkash_CRM
Hi Robert,

You need to use the Relationship name between the object and Account object in order to get the Accont Name, for example if the Lookup field Tenant__c is present on an Object with API name XZY__c below query can be used to get the Account Name.

Select Tenant__r.Name from XZY__c

Also you can refer the below documentation for more understanding on Relationship queries:

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using

Regards,
Akash

All Answers

Akash_CRMAkash_CRM
Hi Robert,

You need to use the Relationship name between the object and Account object in order to get the Accont Name, for example if the Lookup field Tenant__c is present on an Object with API name XZY__c below query can be used to get the Account Name.

Select Tenant__r.Name from XZY__c

Also you can refer the below documentation for more understanding on Relationship queries:

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using

Regards,
Akash
This was selected as the best answer
Robert RobinsonRobert Robinson
Thank you for the answer, and just an important, the documentation that I can refer to going forward (teach me to fish :-) )