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
Matt FolgerMatt Folger 

Ascending Order for a User-Generated SOQL query

I've got a front-end, back-end page that takes user input and creates and then displays a SOQL query.  In this the date order is displaying descending.  Can we get it to display in ascending order?  Better yet, if they could click the title of the display column to order it by that critieria that would be even better.
Best Answer chosen by Matt Folger
Phillip SouthernPhillip Southern
No problem, Order By needs to be at the end, but before Limit.  That link I posted, if you look in the left hand side the reference will tell you which order the keywords for SOQL should appear.

SELECT name, Inquiry__c, Inquiry_Status__c, Product__c, PO__c, Vendor__c, Inquiry_Date__c FROM Vencorr__c  WHERE Inquiry_Status__c like '%Original%' AND Inquiry_Date__c = LAST_N_DAYS: 180 ORDER BY Inquiry_Date__c ASC NULLS LAST LIMIT 1000

All Answers

Phillip SouthernPhillip Southern
In soql you can specify the order by the Order By keyword along with ASC or DESC.

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/index_Left.htm#StartTopic=Content/sforce_api_calls_soql_select_orderby.htm
Matt FolgerMatt Folger
Thanks for the quick response.  So I edited that to add to my SOQL query and when I run a test query I get the following error

ERROR 

Error:
unexpected token: WHERE

That comes from my SOQL query string looking like this
SOQL: SELECT name, Inquiry__c, Inquiry_Status__c, Product__c, PO__c, Vendor__c, Inquiry_Date__c FROM Vencorr__c ORDER BY Inquiry_Date__c ASC NULLS LAST WHERE Inquiry_Status__c like '%Original%' AND Inquiry_Date__c = LAST_N_DAYS: 180 LIMIT 1000

Phillip SouthernPhillip Southern
No problem, Order By needs to be at the end, but before Limit.  That link I posted, if you look in the left hand side the reference will tell you which order the keywords for SOQL should appear.

SELECT name, Inquiry__c, Inquiry_Status__c, Product__c, PO__c, Vendor__c, Inquiry_Date__c FROM Vencorr__c  WHERE Inquiry_Status__c like '%Original%' AND Inquiry_Date__c = LAST_N_DAYS: 180 ORDER BY Inquiry_Date__c ASC NULLS LAST LIMIT 1000
This was selected as the best answer
Matt FolgerMatt Folger
Thanks!  Worked like a charm.