0

Is there a way to do custom order by instead of ASC or DESC?

There is a picklist field -> Color__c on a custom object - Products__c - which has following values:

  1. RED
  2. YELLOW
  3. BROWN

If i write a SOQL: Select id, LastOrderDate__c,Color__c from Products__c where Name = 'Shoes' ORDER BY Color__c DESC

its returning YELLOW,RED,BROWN records in this order

but i want a custom order by which i prioritize RED over YELLOW, is there way do a custom order by instead of ASC or DESC?

2

No, you cannot do that in SOQL.

You can, if you wish, create a new formula field on your object that maps colors to their desired ordering keys, using something like a CASE() formula. You can then order by that formula field.

Note of course that that may come with performance implications, and you'll have to be aware of the formula character limit depending on how many values you have.

0

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.