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
iceberg4uiceberg4u 

how do i make a query statement dynamic

my controller class
public class NewOne
{
        
    List<ContactUs__c> a=new List<ContactUs__c>();
    
        
        public NewOne()
        {
            a=[select name,lastName__c from ContactUs__c];
        }
        public void sort1()
        {
            String val=System.currentPageReference().getParameters().get('st'); //m trying to pass the name of the custom      field                                                                                               
            System.debug('-------------------------------------------------------------');
            System.debug('the value that i have is:'+val);
            System.debug('-------------------------------------------------------------');
            a=[select name,lastName__c from ContactUs__c order by val  asc];     
        }
        public List<ContactUs__c> getDataValue()
        {
            return a;
        }

}
how do i use this "val" that stores a custom field like name,lastname__c in querying or for that matter just accessing data.

smthn like:- ContactUs__c a1=new ContactUs__c();
a."val"=XXX;
how do i use this val ??
from my page i have passed

<apex:outputText value="firstname"/>
                <apex:actionSupport event="onclick" action="{!sort1}" rerender="tb">
                    <apex:param name="st" value="name"/>
                </apex:actionSupport>
hisrinuhisrinu
Have u tried by placing the colon(:) before val?
iceberg4uiceberg4u
yes i have tried that even then it does not work.
craigmhcraigmh

You can't use a variable in the order by clause unless you convert to fully dynamic SOQL. Another major missing feature.

 

Here's what you'd have to do:

a=(List<ContactUs__c>)database.query('select name,lastName__c from ContactUs__c order by ' + val + ' asc');