You need to sign in to do that
Don't have an account?

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);
}
}
}
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);
}
}
}
use this apex class
Here is the code

Hi,
Now, only Constructor code is extra and i am getting the same issue.
Thanks,Bijay

Thank yoiu so much. I found the mistake which i have done.
Great !