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

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?

share|improve this question
up vote 2 down vote accepted

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 = - 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".

share|improve this answer

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.

share|improve this answer

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.