[SalesForce] SOQL NEXT_N_DAYS Query not working

The below query doesn't return any results

select Phone__c from Application__c where Orientation_Date__c < NEXT_N_DAYS:8 and Orientation_Date__c > NEXT_N_DAYS:1

But the next query returns 2 records with Orientation_Date__c 2017-07-27 and 2017-07-28

select Phone__c from Application__c where Orientation_Date__c > NEXT_N_DAYS:1

Today is 2017-07-25 so these 2 records should meet the criteria for the first query right? Why aren't they being returned?

Best Answer

Take a look at the documentation on Date Literals

Date Literal
NEXT_N_DAYS:n

Range
For the number n provided, starts 00:00:00 of the current day and continues for the next n days.

Example
SELECT Id FROM Opportunity WHERE CloseDate > NEXT_N_DAYS:15

Since the range starts from midnight today, using < NEXT_N_DAYS:n is equivalent to < TODAY.

Unfortunately, unlike N_DAYS_AGO:n, there does not appear to be any sort of N_DAYS_FROM_NOW:n literal, so I'm not sure if there is a workaround in pure SOQL. Your best bet might be to create a formula for Days_From_Now__c and filter on that instead.

Related Topic