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
fdacruzfdacruz 

What's wrong with this soql query?

fdacruzfdacruz
This is the SOQL quiry in question:

select name, AccountLookup__r.OwnerId, ownerid from lead where (ownerid <> AccountLookup__r.OwnerId)
karthikeyan perumalkarthikeyan perumal
Hello, 

Store the lookup relationship field in any variable and compare it. like below
String AccOwnerLkp=[Select AccountLookup__r.OwnerId from lead];

select name, AccountLookup__r.OwnerId, ownerid from lead where ownerid <> : AccOwnerLkp

the above code will work. 
hope this helps you. 

Thanks
karthik


 
Alain CabonAlain Cabon
Salesforce doesn't allow direct field to field comparison in SOQL query.

https://help.salesforce.com/articleView?id=000187460&type=1

One solution is a new formula field that will compare fields and return a value (like true or false) which you may use in a WHERE clause.

Formula field (checkbox): IsNotEqualOwnerWithAccountOwner__c  : IF(ownerid != AccountLookup__r.OwnerId, true, false) 

select name, AccountLookup__r.OwnerId, ownerid from lead where IsNotEqualOwnerWithAccountOwner__c = true