Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I compare a date field?

I have 2 objects ObjectA and ObjectB and I would like to run a query for ObjectB that is DateFieldB = DateFieldA.

Here is my Line:

for(ObjectA__c A : Trigger.New)
        if(A.DateFieldA__c != NULL)
            Map<Id, ObjectB__c> B = new MapMap<Id, ObjectB__c> 
                SELECT Id, DateFieldDateB__c
                FROM ObjectB__c
                WHERE DateFieldB__c = A.DateFieldA__c

I am having this error:

Error: Compile Error: expecting a colon, found A.DateFieldA__c at line 13 column 40.
share|improve this question
up vote 2 down vote accepted

Store the date from object A into a temporary variable:

Date tempDate = A.DateFieldA__c;

and then reference this variable in the SOQL query (note the semicolon)

[SELECT Id FROM ObjectB__c WHERE DateFieldB__c = :tempDate];

share|improve this answer
hmmm ... sounds logical. let me give it a try. – JHONG Jul 21 '14 at 4:48
Hi @Bachovski, since DateFieldA__c is actually a Datea/Time field. Can I just do Date TempDate = DATEVALUE(A.DateFieldA__C)? Thanks Again. – JHONG Jul 21 '14 at 5:01
No you need to do: tempDate =; - Check the datetime methods -… – Boris Bachovski Jul 21 '14 at 5:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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