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
JamesSSJamesSS 

self lookup accounts in Account object in soql

How to get the self lookup accounts in Account object in soql?

 

List <Account> AccountsList = [ SELECT Id,
Name,LastActivityDate
(select Id,Name,Owner.Name,Owner.Alias from ParentAccount)
FROM Account
WHERE ( ParentId = null)
];

 

In above soql is wrong.. please any one help to get this original soql?

sandeep@Salesforcesandeep@Salesforce

List <Account> AccountsList = [ SELECT Id, Name,LastActivityDate (select Id,Name,Owner.Name,Owner.Alias from CHILDRELATIONSHIPNAME__r )  FROM Account WHERE id = : ACCOUNTID ];

 

CHILDRELATIONSHIPNAME  is a name which you can get from custom field ( lookup of Account).

 

in this case you have id of account ACCOUNTID 

JamesSSJamesSS

Thanks sandeep.

 

But i need to query the all parent accounts without ACCOUNTID.Is it possible?

hpereirahpereira

Hello,

 

Use this instead:

 

select Id, Name, Owner.Name, Owner.Alias
from Account
where Parent.Id = null

 

Regards

hpereirahpereira
Hello JamesSS,
Could you give kudos to my reply (click the star), if it helped you?
Thanks,
Hugo