2

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.

1 Answer 1

2

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];

3
  • hmmm ... sounds logical. let me give it a try.
    – JOE
    Commented Jul 21, 2014 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.
    – JOE
    Commented Jul 21, 2014 at 5:01
  • No you need to do: tempDate = A.DateFieldA__c.date(); - Check the datetime methods - salesforce.com/us/developer/docs/apexcode/Content/… Commented Jul 21, 2014 at 5:04

You must log in to answer this question.

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