+ Start a Discussion
Amita TatarAmita Tatar 

getting sobject label on vf page dynamically in salesforce

Hi all,

I have a requirement where i want to display field labels from an object on VF page form with checkboxes.
I  developed following code
Apex Class:


public class getLabel{

public List<String> AllLabels{get;set;}
public getLabel(ApexPages.StandardController controller) {
AllLabels = new List<String>();
}


String type = 'ProposalFields__c';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(type);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
//system.debug('fieldmap:'+fieldMap.values());
//AllLabels.add(fieldMap.get(fieldName).getDescribe().getLabel());

}


VF page:


<apex:page showHeader="false" sidebar="false" standardController="Proposal_Form__c" extensions="getLabel">
<apex:form >
<style>
.panelWrapper .mainTitle {
   
    
}
</style>
<apex:outputPanel styleClass="panelWrapper" layout="block">
<apex:pageBlock title="Proposal Form">
<apex:pageBlockSection title="Service Requirements">
<apex:inputField value="{!Proposal_Form__c.Service_Family__c}"/>
<apex:inputfield value="{!Proposal_Form__c.Service_Type__c}"/>
<apex:inputField value="{!Proposal_Form__c.Sub_Service_Category__c}"/>
</apex:pageBlockSection>

<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Save" action="{!Save}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Available fields">
<apex:pageBlockTable var="lab" value="{!AllLabels}">
<apex:column headerValue="Labels">
  {!lab}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>

<!--<apex:pageblockSection title="Fields Selected">
<apex:inputcheckbox label="First Name" />
<apex:inputCheckbox label="Last Name"/>
<apex:inputcheckbox label="Mobile Number"/>
<apex:inputCheckbox label="State"/>
<apex:inputCheckbox label="City"/>
<apex:inputcheckbox label="Pincode"/>
</apex:pageblockSection>-->
</apex:pageBlock>
</apex:outputPanel>

</apex:form> 
</apex:page>

for it,but it is giving error. Please help me for the same.

Thanks & Regards,
Amita Tatar
8805077410
 
NagendraNagendra (Salesforce Developers) 
Hi Amita,

It is simple.
It ıs simple.. 
public List<String> AllLabels{get;set;}
//Do not forget to get instance in constructor for this list
 String type='Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(type);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

for (String fieldName: fieldMap.keySet()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
 
AllLabels.add(fieldMap.get(fieldName).getDescribe().getLabel());//It provides to get the object fields label.
}
 
<apex:pageBlockTable var="lab" value="{!AllLabels}">
<apex:column headerValue="Labels">
   {!lab}
</apex:column>
</apex:pageBlockTable>
Please let us know if this resolved the issue.

Mark this as solved if the information was helpful.

Regards,
Nagendra.
 
Amita TatarAmita Tatar
Hi Nagendra,

I tried with above code, but it is showing error for 'for loop' and line no 12.


Regards,
Amita Tatar