I am able to run a query with a where clause

Opportunity.CloseDate = LAST_N_DAYS:7

However, I have a custom date attribute for custom object. When I try to set this value in memory:

digest.DM_Digest_date__c = LAST_N_DAYS:7;

I get:

expecting a semi-colon, found ':'

Any ideas?


The date constants/literals make sense only in the context of a query. If by "in memory" you mean Apex code, then something like this is needed:

digest.DM_Digest_date__c = System.today() - 7;


digest.DM_Digest_date__c = Date.newInstance(2012,12,31);

You can read up more about Date class's methods (plus System class has today() and now() methods too).

Not to mention the fact that LAST_N_DAYS etc all cover timespans, not actual points in time... They're really just a way to quickly write WHERE CreatedDate > :startPoint AND CreatedDate < :endPoint. So you can't really assign this to one date/time variable, you'd need 2 fields if you want to record a date "from-to".


I am able to query on a custom date field okay

Select Id, Name from Opportunity where Lookup__r.Start_Date__c = LAST_N_DAYS:7

Can you double check that it is indeed a Date field. Also, that the relational reference is correct (does it need an __r ?)

Perhaps, it might also be an idea to post your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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