function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andreas WissmeyerAndreas Wissmeyer 

SOQL query returning something when it shouldn't

Hello all,

I have a list of accounts for which I am the product owner. I modified them all today for testing purposes, in order to have my custom field Last_Update__c = today.

In my VF page, I call my custom constructor which does something very simple (please note that I removed the code handling the exceptions for brevity reasons
 

public Account getAccount() 
{
    account = [SELECT Id, Name, Last_Status__c,Additional_Information__c,Ongoing_Problem__c,Last_Update__c FROM      Account WHERE (OwnerId = :UserInfo.getUserID()) AND (Type <> 'Dead') AND (Last_Update__c = LAST_N_DAYS:150) 
ORDER BY Last_Update__c LIMIT 1];
     return account;
 }

The idea should be to retrieve an account only when three conditions are all met. Third one is 
Last_Update__c = LAST_N_DAYS:150. In my understanding, this should mean: "select an account if Last Update was done more than 150 days ago". However, I get every account in the list as a result, even if they all have today as last_update__c field. What am I doing wrong?

Thanks in advance for your help on this.

Cheers, A.

Best Answer chosen by Andreas Wissmeyer
Shailendra Singh ParmarShailendra Singh Parmar
If you are looking for record older than 150 then you should be using greathan symbol in query. try something like
public Account getAccount() 
{
    account = [SELECT Id, Name, Last_Status__c,Additional_Information__c,Ongoing_Problem__c,Last_Update__c FROM      Account WHERE (OwnerId = :UserInfo.getUserID()) AND (Type <> 'Dead') AND (Last_Update__c < LAST_N_DAYS:150) 
ORDER BY Last_Update__c LIMIT 1];
     return account;
 }

 

All Answers

Art SmorodinArt Smorodin
Hi Andreas, 

Using LAST_N_DAYS:150 in SOQL will return all records where the date starts from 12:00:00 AM (user Local Time Zone) of the current day and continues for the last 150 days. So it is basically a rage. Start data is 150 days ago and end date is today.

Check out this site, it has some good details about using date filtes in SOQL: http://simplysfdc.blogspot.com/2013/11/salesforce-soql-lastweek-lastndaysn-and.html

Art.
Krishna SambarajuKrishna Sambaraju
Try changing query as Last_Update__c < LAST_N_DAYS:150
Shailendra Singh ParmarShailendra Singh Parmar
If you are looking for record older than 150 then you should be using greathan symbol in query. try something like
public Account getAccount() 
{
    account = [SELECT Id, Name, Last_Status__c,Additional_Information__c,Ongoing_Problem__c,Last_Update__c FROM      Account WHERE (OwnerId = :UserInfo.getUserID()) AND (Type <> 'Dead') AND (Last_Update__c < LAST_N_DAYS:150) 
ORDER BY Last_Update__c LIMIT 1];
     return account;
 }

 
This was selected as the best answer
Andreas WissmeyerAndreas Wissmeyer
Thanks a lot for your help. I had already tried this solution but I could not handle correctly the results. I have still to work on exceptions handling, but your answer gave me the confirmation I should work with this SOQL entry. Have a nice day, A.