2

Im writing a query in a Batch Apex Job.

I want to return records whose parent account field is NULL from Account object.

The Parent Account is a Hierarchy type. Its Label is "Parent Account" and field name is "Parent".

The following query gives error.

'select ID,Name,Parent,Region__c,Bi_Annual_Revenue__c from Account where Parent.id = null';

18:44:29.064 (64189694)|EXCEPTION_THROWN|[27]|System.QueryException: No such column 'Parent' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Please let me know how to refer to the parent field in the query.

Thanks,

Priya

I removed the parent field from the select statement and its working. I dont need that.

Thanks for the responses.

1
  • Try ParentId instead of Parent.id Jan 8, 2015 at 13:24

3 Answers 3

2

Trying to match on Parent.Id=null seems an odd choice because it both implies that the lookup will exist, while also implying that we want a record that somehow has a null Id. What we actually want is an Account record with a null value in this lookup field, like so:

select ID,Name,Region__c,Bi_Annual_Revenue__c from Account where ParentId= null

1

ParentId is the field you should be using in your query. The query below should work.

It is irrelevant in this example because this query will only return Accounts without parent records but I have also included how you would query fields from the parent account.

select 
    Id,
    Name,
    ParentId,
    Parent.Name,
    Parent.Region__c,
    Region__c,
    Bi_Annual_Revenue__c
from Account
where ParentId = null
-1

The problem is not Parent.Id but the Parent field in the field list. Either you query ParentId (which is a field on Account) or you query any of the fields in the related Parent Account by using e.g. Parent.Id or Parent.Name, etc.

So change

'select ID,Name,Parent,Region__c,Bi_Annual_Revenue__c from Account where Parent.id = null';

to e.g.

'select ID,Name,Parent.Id,Region__c,Bi_Annual_Revenue__c from Account where Parent.id = null';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .