2

I have a problem with sorting data with SOQL query.

I am sorting data with

SELECT Id, Name, CreatedDate, Status FROM Account ORDER BY CreatedDate ASC;

But i want to add one more field to sort with.

I need to make output sorted by CreatedDate and put all records with Status = 32' at the end of the list.

5

I believe you're looking for this:

SELECT Name, CreatedDate, Status FROM Account ORDER BY Status, CreatedDate

This will put all the same Status values next to each other, which each record in the group in ascending CreatedDate order.

If you want to have everything in CreatedDate order, except for the Status of 32 specifically, then you'd need two different queries, or create a formula field that you could sort on. You could create a formula for this:

IF(Status='32','Yes','No')

Then you can sort on it:

SELECT Name, CreatedDate, Status FROM Account ORDER BY IsStatus32__c, CreatedDate

In the general case, if you need this very specific type of behavior, you would need multiple queries or process the records after querying them.

1
  • Thank you, the solution with the formula made a trick! :) – David Oct 20 '18 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.