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
Bijay SahuBijay Sahu 

Binding dynamic fields into pageBlockTable is not working

Hi All,
I am new to salesforce Apex page and class. I have written a Apex class to prepare a dynamic query based on user input. In my apex page i have two inputbox where user can input filed name. After that by clicking on a command button i am getting those values and making a dynamic query and executing. after executing the data should populate in frontend.

If i am testing Apex class from  Anonymous window. Apex class is working fine where as if i try to bind the query returned values in Apex page it is throwing me an error. "Invalid field id, for SObject Account "

Could anyoine help me here.

Apex page
<apex:page controller="DynamicQuery_ctrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages/>
            <apex:pageBlockSection columns="1">
                <apex:inputText value="{!firstValue}" label="Field Name"/>
                <apex:inputText value="{!secondValue}" label="Field Name"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Show" action="{!showValues}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:repeat value="{!lstFields}" var="fieldName">
                    <apex:column value="{!acc[fieldName]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
public class DynamicQuery_ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
            
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
      
    public void showValues()
    {
        try{
            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id'+',');
            lstFields.add(firstValue+',');
            lstFields.add(secondValue);
            system.debug(lstFields);
                    
            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);
            
            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            system.debug(accList);
                       
        }catch(Exception ex){
         ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
         ApexPages.addMessage(msg);          
      }
        
    }
}
Raj VakatiRaj Vakati
use this apex class
 
public class DynamicQuery_ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
    
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
    
    public void showValues()
    {
        try{
            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id');
            lstFields.add(firstValue);
            lstFields.add(secondValue);
            system.debug(lstFields);
            
            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);
            
            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            system.debug(accList);
            
        }catch(Exception ex){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);          
        }
        
    }
}

 
Raj VakatiRaj Vakati
Here is the code
 
<apex:page controller="DynamicQuery_ctrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
                <apex:inputText value="{!firstValue}" label="Field Name"/>
                <apex:inputText value="{!secondValue}" label="Field Name"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Show" action="{!showValues}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:repeat value="{!lstFields}" var="fieldName">
                    <apex:column value="{!acc[fieldName]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class DynamicQuery_ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
    
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
	
	
	public DynamicQuery_ctrl(){
		firstValue = 'Type' ; 
		secondValue = 'Rating' ; 
        showValues();
		
	}
	
    
    public void showValues()
    {
        try{
            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id');
            lstFields.add(firstValue);
            lstFields.add(secondValue);
            system.debug(lstFields);
            
            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);
            
            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            system.debug(accList);
            
        }catch(Exception ex){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);          
        }
        
    }
}

 
Bijay SahuBijay Sahu
Hi, Now, only Constructor code is extra and i am getting the same issue. Thanks,Bijay
Bijay SahuBijay Sahu
Thank yoiu so much. I found the mistake which i have done.
Raj VakatiRaj Vakati
Great !