+ Start a Discussion
Michael MMichael M 

Order by not working in SOQL query

Hello, I am trying to query by Discharge__c (custom object) record with the most recent discharge_date__c. 

This is the query I am using:

    Discharge__c dis = [select id, name_dob_formula__c, date_of_birth__c, discharge_date__c from discharge__c where Name_DOB_formula__c =: namePlusDob order by discharge_date__c desc limit 1];    

However it does not seem to be working. It is querying old discharge__c records with later discharge_date__c's  Is there something I should change in the query?
 
Best Answer chosen by Michael M
VinayVinay (Salesforce Developers) 
Hi Michael,

Do you see any errors or no results?

Can you try removing limit and desc.  'Name_DOB_formula__c' is used to calculate dates or what it does?

Thanks,

All Answers

VinayVinay (Salesforce Developers) 
Hi Michael,

Do you see any errors or no results?

Can you try removing limit and desc.  'Name_DOB_formula__c' is used to calculate dates or what it does?

Thanks,
This was selected as the best answer
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/190918/soql-order-by-is-not-working-properly

Maybe the above discussion assists you.

For further reference check this too,

https://salesforce.stackexchange.com/questions/7667/soql-result-ordering-in-the-absence-of-an-order-by-clause/7669
Michael MMichael M
Thank you both. It turns out the issue was with my name_dob_formula__c field. When I fixed that piece, it worked properly.