0

I need to get all Assets X with X.ParentId != NULL, and the X.Parent.AccountId != X.AccountId.

SELECT AccountId,Id,ParentId,Parent.AccountId,RecordTypeId,RootAssetId FROM Asset WHERE ParentId != NULL AND Parent.AccountId != AccountId AND RecordType.DeveloperName='RecTypeABC' limit 10

The SOQL above fails with a MALFORMED_QUERY error.

Any idea how to do this?

Thanks

0

1 Answer 1

2

You cannot make a field to field comparison in SOQL i.e. as mentioned in the question, you cannot compare X.Parent.AccountId != X.AccountId.

There is other workaround by which you can do this i.e. by creating a new formula field which evaluates to true when X.Parent.AccountId = X.AccountId and false when it is not same.

The formula for your new field would be

IF(X.Parent.AccountId = X.AccountId, 'true', 'false')

And then you can query for records where this new formula field is 'false'

So your query would become

SELECT AccountId,Id,ParentId,Parent.AccountId,RecordTypeId,RootAssetId FROM Asset WHERE ParentId != NULL AND New_Formula_Field__c='false' AND RecordType.DeveloperName='RecTypeABC' limit 10

You can refer Field to field comparison in WHERE clause of SOQL for further help on this topic.

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