0

In the Anonymous Window of the developer console, I am attempting to execute the following:

System.debug(
    JSON.serialize([
        SELECT OpportunityLineItemSchedule.Revenue, OpportunityLineItemSchedule.ScheduleDate, OpportunityLineItemSchedule.OpportunityLineItem.TotalPrice, OpportunityLineItemSchedule.OpportunityLineItem.Opportunity.Id, OpportunityLineItemSchedule.OpportunityLineItem.Opportunity.CloseDate, OpportunityLineItemSchedule.OpportunityLineItem.Opportunity.Amount
        FROM OpportunityLineItemSchedule
        WHERE OpportunityLineItemSchedule.OpportunityLineItem.Opportunity.Amount != OpportunityLineItemSchedule.OpportunityLineItem.TotalPrice
    ])
);

I receive this error:

Line: 5, Column: 78
expecting a colon, found 'OpportunityLineItemSchedule.OpportunityLineItem.TotalPrice'

I understand this error typically pops up when one is attempting to use a variable in a query. However, I am not: I am using standard fields. Why am I receiving this error?

2
  • Are you sure it's an error in the query itself? I think it might be an issue with JSON.serialize() instead (no idea on how to fix that if it is the case). Can you try debugging the query without serializing it?
    – Derek F
    Commented Sep 11, 2017 at 17:41
  • 1
    Refer this knowledge article by Salesforce: Field to field comparison in WHERE clause of SOQL Commented Sep 11, 2017 at 17:44

1 Answer 1

5

You can't have a field reference on the right side of a filter; SOQL, while similar to SQL, is definitely not SQL.

The following example is also illegal:

SELECT Id 
FROM Opportunity
WHERE Account.Name = Name

If you need to compare two fields together, you need to make a custom formula field, or query the records (potentially all of them), and compare them to each other. Assuming I remember correctly, the Schedule objects don't support custom fields, including formulas.

Note that any way you go about it, you're looking at a full table scan, so expect problems if you have a large number of records. You might want to narrow down your search via other techniques (such as filtering on an account name or something).

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