3

I am trying to run this query:

SELECT Id, Account.Primary_Email__c, Email FROM Contact where Primary_Indicator__c = true AND  LastActivityDate = Last_week AND Email != Account.Primary_Email__c LIMIT 200

I am getting the following error:

Bind variables only allowed in Apex code

I would really appreciate if any one can explain why this query works in apex and cannot be run directly in SOQL editor.

2 Answers 2

1

The error thinks you are attempting to use a bind variable, from the docs...

SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind.

Which is allowed in apex but using another field as a value is not as Keith pointed out in his answer.

Instead what you can do is create a formula on the Contact object that compares the email to the account primary email then use that comparison field in your query.

There is an idea for this type of comparison in reports that is under consideration out on the idea exchange here, but it looks like it may be a few releases out yet.

2

The field expression syntax in a SOQL where clause is:

fieldName comparisonOperator value

where value is limited:

A value used to compare with the value in fieldName ... You must supply a native value—other field names or calculations are not permitted ...

so your field expression:

Email != Account.Primary_Email__c 

should not work anywhere.

You must log in to answer this question.

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