Is it possible to to compare the parent id of an account to another account? i.e.

SELECT Id from Account WHERE child_account.parentId = parent_account.Id;

I keep getting the 'Unknown Error parsing query' message from the Query Editor.


You cannot compare a field to another field in SOQL. The standard workaround is to use a formula. It's hard to tell what you're trying to achieve based on your syntax, but if you wanted to filter on records where one field equals another, you would do make a formula (say DoesField1EqualField2__c) that looks like:

Field1__c = Field2__c

And then in your query, filter on that:

SELECT Id FROM Account WHERE DoesField1EqualField2__c = true
| improve this answer | |
  • I've adjusted the syntax in my most recent edit. – Kai Tribble May 4 '17 at 17:01
  • @KaiTribble So in your case Field1__c would be replaced with ParentId and Field2__c would be replaced with Parent.Id. Though I'm not sure how you think these two values could ever differ. – Adrian Larson May 4 '17 at 17:04
  • Neither do I. However, there is a corresponding validation rule that has this criteria listed: if(child_account.id != parent_account). Note they are both accounts, so they aren't in a master-detail relationship. – Kai Tribble May 4 '17 at 19:01


[SELECT Id from account where child_accountId = :parent_account];

Since both child account Id and Parent_account are on account object, you can compare. parent_account is a variable that holds parent account Id. It will be assigned as follows,

string parent_account = account.accountId;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.