0

I have created controller and visual force pages. In my controller using field sets it's working fine. My VF pages having problem for unknown property. What should I need to change in my Visual force page and controller. I mention below error line in my VF page.

public with sharing class PageBlockTableFieldSetExample {
    public List<Account> accounts { get; private set; }
    public List<String> headers { get; private set; }
    public List<RowExample> rows { get; private set; }
    //public string accounts1{get;set;}

    public PageBlockTableFieldSetExample() {
        String queryFields = '';
        for(Schema.FieldSetMember f : SObjectType.Account.FieldSets.Example_Fieldset.getFields()) {
            if(queryFields.length() > 0) {
                queryFields += ', ';
            }
            queryFields += f.getFieldPath();
        }

        //Query the records you want to display
        this.accounts = Database.query(
            ' SELECT ' +
                queryFields +
            ' FROM ' +
                ' Account ' +
            ' WHERE ' +
                ' AccountNumber = null ' +
            ' LIMIT 10 ');


        //Setup the row of classes example
        this.headers = new List<String> { 'Monday', 'Tuesday', 'Wednesday' };
        this.rows = new List<RowExample>();
        for(Integer i = 0; i < 3; i++) {
            this.rows.add(new RowExample(this.headers.size()));
        }
    }

    public class RowExample {
        //A variable number of cells in different rows will result in 
        public final List<String> cells { get; private set; }

        public RowExample(Integer length) {
            this.cells = new List<String>();
            for(Integer i = 0; i < 3; i++) {
                this.cells.add('Cell ' + (math.random() * 10).intValue());
            }
        }
    }
}

VF Page:

<apex:page>
<apex:form>
<apex:pageBlock id="block">
    <apex:pageBlockTable id="table" var="account" value="{!accounts}">//error Line
        <apex:column headerValue="Account Name" value="{!account.Name}" />
        <apex:repeat var="f" value="{!$ObjectType.Account.FieldSets.Example_Fieldset}">
            <apex:column headerValue="{!f.label}">
                <apex:inputField value="{!account[f]}" />
            </apex:column>
        </apex:repeat>
   </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

3 Answers 3

2

You are not using the controller in your page.

In your visualforce page, include the controller in the apex:page

<apex:page controller="PageBlockTableFieldSetExample" >

UPDATE :

for the SOQL error you mentioned in the comment, it seems the final SOQL generated is not having proper syntax. my guess is its having an additional comma in the list of fields before FROM.

if that's the case you can remove the extra comma after the last field using below code before you use it in the Database.Query method

if(queryFields.endsWith(','))
  queryFields = queryFields.removeEnd(',');     

TIP : whenever you see such errors, you should use proper exception handling and debug statements to log your variables and any exceptions to see what's causing the error.

also, here's a reference on how to use fieldsets in dynamic SOQL

3
  • @ Vamsi Krishna. Thanks for your response I really want to say sorry for forget create controller..
    – Nikitha
    Commented Aug 24, 2015 at 10:15
  • now I am getting this error:System.QueryException: unexpected token: 'FROM' Class.streamingAPI.PageBlockTableFieldSetExample.<init>: line 17, column 1
    – Nikitha
    Commented Aug 24, 2015 at 10:17
  • Use System.debug(); to output and debug the query string you're passing to Database.query();. It may be that queryFields is blank so you're just passing SELECT FROM [...].
    – Rob
    Commented Aug 24, 2015 at 10:23
0

Sorry I can't able to comment on above question so posting as an answer

You need to remove last ", " to be added from below line

queryFields += ', ';

Otherwise if your queryFields contains id and name than your query become

select id, name, from Account where accountnumber = null limit 10

4
  • I initially thought that too, but it's adding queryFields += ', '; before the field name if the string already contains a field name.
    – Rob
    Commented Aug 24, 2015 at 10:33
  • Can you tell me exactly place it.. please check my query:this.accounts = Database.query('SELECT '+ queryFields += ', '; +'FROM ' + ' Account ' + ' WHERE ' + ' AccountNumber = null ' + ' LIMIT 10 ');
    – Nikitha
    Commented Aug 24, 2015 at 10:56
  • may be you can update your code as below Commented Aug 24, 2015 at 11:10
  • @Ritesh Gupta again having query problem. What do I need to change my query.
    – Nikitha
    Commented Aug 24, 2015 at 11:16
0

I have updated few lines in the code as below

 String queryFields = '';
    boolean flag = false;
    for(Schema.FieldSetMember f : SObjectType.Account.FieldSets.Example_Fieldset.getFields()) {
        if(flag){
            queryFields += ', ' + f.getFieldPath();
        }
        else{
            queryFields += f.getFieldPath();
            flag = true;
        }
    }

    if(queryFields != ''){
        //Query the records you want to display
        this.accounts = Database.query(
            ' SELECT ' +
                queryFields +
            ' FROM ' +
                ' Account ' +
            ' WHERE ' +
                ' AccountNumber = null ' +
            ' LIMIT 10 ');
    }

    //Setup the row of classes example
    this.headers = new List<String> { 'Monday', 'Tuesday', 'Wednesday' };
    this.rows = new List<RowExample>();
    for(Integer i = 0; i < 3; i++) {
        this.rows.add(new RowExample(this.headers.size()));
    }

If the above code didn't show any account than you may not have added any field in mentioned field set.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .