Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
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. –  Phil Hawthorn Dec 4 '12 at 8:35
    
I have updated the question with attaching generated query and query seems to be ok –  Pramod Kumar Dec 4 '12 at 8:44
1  
The query from the debug output is using labels, e.g. "Application Started", not api names, which is not ok :-) –  Stephen Willcock Dec 4 '12 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. –  Phil Hawthorn Dec 4 '12 at 8:51
add comment

1 Answer 1

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().

share|improve this answer
    
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 –  Pramod Kumar Dec 4 '12 at 9:00
    
What does the query look like without the lables? –  Bob Buzzard Dec 4 '12 at 9:03
    
SELECT Status_Application_Started__c, Status_Application_Submitted__c, Status_Application_Complete__c, Id FROM Application__c where id ='a0BJ0000002kIbLMAU' –  Pramod Kumar Dec 4 '12 at 9:25
2  
Would that not just be f.label instead of con[f.label] ? –  Phil Hawthorn Dec 4 '12 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]} –  logontokartik Dec 4 '12 at 11:20
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.