Allow use of bind variable in SOQL "ORDER BY" clause - Ideas - Salesforce Trailblazer Community
Trailblazer Community

All Ideas

Idea Details

Post an Idea
80  Points
Idea has been posted. Give it an upvote or downvote.

Allow use of bind variable in SOQL "ORDER BY" clause

Accounts & Contacts

It is common for fully featured user interfaces to allow user-selectable sorting.  In many cases, when dealing with large data sets, it is desirable to allow the database to handle sorting by using an ORDER BY clause in the SOQL query.  However, using a user-driven sort requires manipulating the query text as a string, in order to update the ORDER BY criteria.

Instead, allow bind variables to be used in a SOQL "ORDER BY" clause.  For example:

string sortBy = 'name';
string sortDirection = 'DESC';
list<Account> accounts = [select id, name from Account order by :sortBy :sortDirection];

Alternatively, you could require both the sort field an sort direction to by passed in a single bind variable, like so:

string sortClause = 'name' DESC;
list<Account> accounts = [select id, name from Account order by :sortClause];

I think the first is more flexible, but either would simplify controllers that provide user-selectable sorting.

Merge Idea · Flag

  • Upvotes
  • Downvotes



from AppExchange


Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.