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
HGHG 

select account name from a custom object

Hi all,

I have a custom object called "CO" and having a field called "User".
I am able to get Account ID now, but I would like to get Account Name insted.
Would someone let me know if there is a way to get Account Name from CO?

=================================
CO (CO__c)
----
Id
User (User__c) -> Contact
                            ---------
                            Id
                            Company  (Account) -> Account
                                                               ---------
                                                               Id
                                                               Name
=================================

"User"       : Lookup(Contact)
"Company": Lookup (Account)

This is what I have now.
select User__r.AccountId from CO__c where Id = 'xxxxxxx';

Thank you,
HG
Best Answer chosen by HG
mukesh guptamukesh gupta
Hi,
 Please use below query :-

select User__r.Account.Name from CO__c where Id = 'xxxxxxx';

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi,
 Please use below query :-

select User__r.Account.Name from CO__c where Id = 'xxxxxxx';

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
HGHG
Hi Mukesh,

Thank you for the reply.
I have tried that, but I am getting "[object Object]" as the result.

Do you know how to fix it?

Thanks
HG
Rahul.MishraRahul.Mishra
Hi HG,

Run the below query from dev console, you will get the output in debug. Use similar sytax to fetch the data from list:
 
List<CO__c> lstCo = [Select Id, User__r.Account.Name from CO__c limit 1];
system.debug('Account Name is'+lstCo[0].User__r.Account.Name);

Mark solved if it does help you
HGHG
Hi Mishra,

Thanks for the query.
But I got an error message "The query has to start with 'FIND' or 'SELECT'".

Should I run it from some where else instead of Query Editor?

Thanks
HG
teena jacobteena jacob
Hi,
     Run it on Anonymous Window, it works

List<CO__c> lstCo = [Select Id, User__r.Account.Name from CO__c limit 1];
system.debug('Account Name is'+lstCo[0].User__r.Account.Name);
HGHG
Hi Jacob,

Thank you for the info.
I was able to run it successfully on Anonymouse Window.

Thanks!
teena jacobteena jacob
Hi HG,

        Welcome!