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
Jeff_Rogers.ax1383Jeff_Rogers.ax1383 

SOQL - OpportunityLineItem Sort

I have some apex code that queries the opportunity and opportunitylineitem tables orders by sortorder and then sends an xml off to a back office ERP system to create a sales order.  My users want the order of the lines on the sales order to appear in the same order as the opportunity lines on the opportunity.  Ordering the query results by sortorder works when a user has specified a specific order for the products, otherwise it doesn't work because sortorder is null.  What would the best way to get around this?  Check to see if sortorder is null then us the list.sort function to sort the list by product name?  Any ideas would be appreciated, thanks!

sfdcfoxsfdcfox
ORDER BY SortOrder ASC, CreatedDate ASC

That's how sfdc does it, and that's how'd you do it if you wanted to mimic that behavior.