1

Let's say we have a query

select id,
    history_contact__r.last_seen__c,
    request__c
from history__c 
where lot__r.active__c = true 
and history_contact__c = '0031y000004Kr1AAAS'
and history_contact__r.last_seen__c < request__c

but result is:

'Unknown error parsing query'

Why we cannot compare two fields directly and have to use parameters?

0

1 Answer 1

5

This syntax was not supported when building out the query language. Outside of that design team, few people will be able to answer the "why" of that question. However, the common workaround is to simply implement a formula which does the comparison for you, then filter on that formula instead.

For example, your formula syntax would look like:

history_contact__r.last_seen__c < request__c

And then your query would just change to:

AND History_Contact__c = '003...'
AND My_Descriptive_Field_Name__c = true
4
  • Does it mean that I have to create formula field in history__c object?
    – pincet
    Jan 16, 2020 at 20:45
  • 1
    Yes. You have to put the formula on your child object in this case.
    – Adrian Larson
    Jan 16, 2020 at 20:47
  • Thanks, but what about this workaround? DateTime lastSeen = (DateTime)[select last_seen__c from Contact where id= :contact limit 1][0].last_seen__c; And then (in query) - and request__c >= :lastSeen
    – pincet
    Jan 16, 2020 at 20:50
  • 1
    If you want to add more code and queries to work around the problem, it may be viable. But that sort of approach wouldn't work from a pure SOQL standpoint.
    – Adrian Larson
    Jan 16, 2020 at 20:55

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