2

I want to get field label from a fieldSet. I have given below my component code where i want to display fieldSet Labels only.

<apex:component controller="LeftBarComponentController">   
    <apex:pageBlock >
      <apex:pageblocksection title="Personal Information">  
                <apex:repeat value="{!fields}" var="f">                 
                    <apex:outputText value="{!con[f.label]}">                    
                    </apex:outputText>                    
                </apex:repeat>  
            </apex:pageblocksection>
   </apex:pageBlock>
</apex:component> 

My controller code is as follows -

public Application__c con{get;set;}
//User cid = [Select id,ContactId from User where id =:UserInfo.getUserId()];
String  ids ='003J00000057btk';
Id tempId = ids;
Contact AppId = [select AppId__c from Contact where id=:tempId ];


public LeftBarComponentController(){
    this.con = getApplicationDetails();
}

 public List<schema.fieldsetmember> getFields() {  
    return SObjectType.Application__c.FieldSets.ApplicationStatusFieldset.getFields();  
}  

public Application__c getApplicationDetails() { 
    String query = 'SELECT ';  
    for(Schema.FieldSetMember f : this.getFields()) {  
        query += f.getFieldPath() + ', ' ;  
        query += f.getLabel() + ', ' ; 
    }  
    query = query+'Id FROM Application__c where id =\''+AppId.AppId__c +'\'' ;
    return Database.query(query);  
} 

When i try to run the component i am getting following error -

only aggregate expressions use field aliasing

An unexpected error has occurred. Your development organization has been notified.

Debug shows following query - 03:19:53.060

(60864000)|USER_DEBUG|[25]|DEBUG|#########SELECT Application Started, Application Submitted, Application Complete, Id FROM Application__c where id ='a0BJ0000002kIbLMAU'

Thanks in advance

4
  • 1
    Your query will be wrong because of the way you're building it up in your for loop, try outputting the statement using System.debug and you'll see the issue. Commented Dec 4, 2012 at 8:35
  • I have updated the question with attaching generated query and query seems to be ok Commented Dec 4, 2012 at 8:44
  • 1
    The query from the debug output is using labels, e.g. "Application Started", not api names, which is not ok :-) Commented Dec 4, 2012 at 8:50
  • I must admit, I haven't used fieldsets before but should you be including f.getLabel() as well as f.getFieldPath() ? You are getting the message because of the space between Application and the next token. Commented Dec 4, 2012 at 8:51

1 Answer 1

2

It looks like you are querying the field labels (e.g Application Started) rather than the API names (which I would expect to be Application_Started__c).

The SOQL parser interprets 'Application Started' as an attempt to query back the field 'Application' and alias it to 'Started', hence the error about field aliasing.

You don't need to append the label to the query as well as the result of getFieldPath().

7
  • Got your point still if i make query like this - for(Schema.FieldSetMember f : this.getFields()) { query += f.getFieldPath() + ', ' ; } Still i am getting the same error while trying to print labels on VF page Commented Dec 4, 2012 at 9:00
  • What does the query look like without the lables? Commented Dec 4, 2012 at 9:03
  • SELECT Status_Application_Started__c, Status_Application_Submitted__c, Status_Application_Complete__c, Id FROM Application__c where id ='a0BJ0000002kIbLMAU' Commented Dec 4, 2012 at 9:25
  • 2
    Would that not just be f.label instead of con[f.label] ? Commented Dec 4, 2012 at 11:15
  • 2
    Looks like you have 2 different things going here, try repeating on the <apex:repeat value="{!$ObjectType. Application__c.FieldSets.ApplicationStatusFieldset}" var="fld"> and now you can get the Field Label using fld.Label and value using dynamic binding {!con[fld]} Commented Dec 4, 2012 at 11:20

You must log in to answer this question.

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