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
Rajat MahajanRajat Mahajan 

Vf Variables passed to order by clause in sql queries

Hi All,

 

I am trying to pass a picklist value into a sql query "order by" clause

 

However i am not able to use the syntax :

 

:<variable name> in order by

 

Could you please suggest me a solution to this

 

Regards

Rajat.

Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi Rajat,

 

  In SOQL we cannot use variable  after an ORDER BY clause the solution is to use Dynamic SOQL which provides you the flexibility of creating a query string and execute. Please have a look at the code below .if change the  variable field  to some another field name  the n the selection will be order  by that field.

 

 

String field ='Name';
String s='Select id from Contact Order by '+field ;
List<Contact> conList =Database.query(s);

 

try the above code as your requirement ,feel free for further clarifications.