5

I'm looking for an easy way to get a week by day. So that, I have a function that get in input a day, and return all the accounts created in the week of that day.

So for example if I call the function with, as input the day 15th of January 2015, it get me all accounts created between 12-01-2015 and 18-01-2014 (considering I'm writing dd-mm-yyyy).

Any hints? Is there a declarative way to do it with SOQL similar to THIS_WEEK or LAST_WEEK ?

1 Answer 1

6

EDIT It appears that you might be after something a bit different.

What you could do is (assuming that your input has been converted to a date) is to find the start of the week for that date and query between it and 7 days in advance.

Date startOfWeek = inputDate.toStartOfWeek();
Date endOfWeek = startOfWeek.addDays(7);

List<Account> = [SELECT Name, CreatedDate From Account Where CreatedDate > :startOfWeek AND CreatedDate <= :endOfWeek];

[Original Answer For Reference]

Yes, there is a feature of SOQL called 'Date Literals'. You would perform a query like:

List<Account> = [SELECT Name, CreatedDate From Account Where CreatedDate > LAST_N_DAYS:21];

There are a lot of other Date Literals that you can use. Here is the full reference.

2
  • Hi thank you for the reference. Maybe I was not so clear, please see the edits.
    – mcmc
    May 3, 2015 at 21:44
  • No, you were clear enough - my mistake - I've amended my answer :) May 3, 2015 at 21:47

You must log in to answer this question.

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