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 

Interpretation problems in custom costructor SELECT query

Hello all,

could someone be so kind to explain me the following?

CONTEXT:

CUSTOM CONSTRUCTOR --> CUSTOM VISUALFORCE PAGE --> LIGHTNING APP

In the custom constructor I use the following SOQL query:

public class Account_Controller 
{
    public Account account;
    
    public Account getAccount() 
    {
        account = [SELECT Id, Name, Last_Update__c FROM Account 
                       WHERE (OwnerId = :UserInfo.getUserID()) AND (Last_Update__c = LAST_N_DAYS:150) 
                       ORDER BY Last_Update__c DESC LIMIT 1];
    }
  //BUTTON ACTIONS
   public PageReference OK() 
   {
       account.Last_Update__c = Date.today();
       upsert(account);
    }
}

Code is not complete, but just enough to make you get what I'm doing. In the custom page, when clicking an "OK" button upsert(account) is called, I stay on my custom page and the field Last_Update__c is modified to today's date. Everything works fine, but then I would expect that next time I open my Lightning up I get a different account as a result, seen that condition 
Last_Update__c = LAST_N_DAYS:150      shouldn't be met anymore.

So, why I always get the account I just modified and not a new one wit no updates for more than 150 days? What I am missing/doing wrong?

20000 points to the best answer :)

Seriously, thanks in advance for your help on this. Cheers, A
 

Best Answer chosen by Andreas Wissmeyer
Krishna SambarajuKrishna Sambaraju
I think it is the ordering, as you are ordering in descending order of Last_Update__c, the first record will always be the last updated record. Try changing the order to ascending by removing DESC in the order by clause.

account = [SELECT Id, Name, Last_Update__c FROM Account WHERE (OwnerId = :UserInfo.getUserID()) AND (Last_Update__c = LAST_N_DAYS:150) ORDER BY Last_Update__c LIMIT 1];

By this way the last updated record will last in the above list, and you update the first record which will be different every time.

Check if it works as you expect.
 

All Answers

Krishna SambarajuKrishna Sambaraju
I think it is the ordering, as you are ordering in descending order of Last_Update__c, the first record will always be the last updated record. Try changing the order to ascending by removing DESC in the order by clause.

account = [SELECT Id, Name, Last_Update__c FROM Account WHERE (OwnerId = :UserInfo.getUserID()) AND (Last_Update__c = LAST_N_DAYS:150) ORDER BY Last_Update__c LIMIT 1];

By this way the last updated record will last in the above list, and you update the first record which will be different every time.

Check if it works as you expect.
 
This was selected as the best answer
Shailendra Singh ParmarShailendra Singh Parmar
I think what Krishna is suggesting shoudl work!
Andreas WissmeyerAndreas Wissmeyer
Yes it does work. I feel very stupid right now.. Thanks a lot Krishna! A.