Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

1 Answer 1

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 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 at 5:01
    
No you need to do: tempDate = A.DateFieldA__c.date(); - Check the datetime methods - salesforce.com/us/developer/docs/apexcode/Content/… –  Bachovski Jul 21 at 5:04

Your Answer

 
discard

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.