I have a few apex:pageBlockSectionItem

    <apex:pageBlockSectionItem helpText="{!$ObjectType.Opportunity.fields.TextField__c.InlineHelpText}"
      dataStyleClass="myCSS" labelStyleClass="myCSS"
      <apex:outputLabel value="{!$ObjectType.Opportunity.fields.TextField__c.Label}"/>
      <apex:inputField value="{!opp.TextField__c}" taborderhint="2" styleClass="myCSS" />

on my page. They are identical, with exception of helpText attribute, inputField value and taborderhint. I was looking to put this code into an apex:component, but I stuck with apex:inputField tag. How can I put field in it? Fields have different types.

  • Maybe there is a way to pass an object as first attribute and name of field as second attribute. Then using some inline functions get object's value, basing on field name/ Sep 17 '15 at 13:50

You can use fieldsets for your work .

Based on requirements you will pass different fieldsets

Sample code

   <apex:component Controller="Controller">
 <!-- These attributes are REQUIRED -->
 <apex:attribute type="String" name="sobj" required="true" description="Original poster failed to provide a useful description"/>
<apex:attribute type="String" name="fieldsetName" required="true" description="Original poster failed to provide a useful description"/>

<!-- This is the Markup of the actual component. -->
<!-- An apex:repeat tag iterates over the collection specified by the value attribute
    For each iteration it provides a local variable whose name you assign with the
    var attribute. So, for example: we'll iterate over the records returned
    by our query, and then for a given record, we'll iterate over
    the fields in the fieldsets. -->

 <!-- iterate over each of the records in the records collection -->
<apex:repeat var="record" value="{!records}">
<!-- iterate over each of the fields in the fieldset -->
<apex:repeat var="field" value="{!fieldsets}"> 
    <!-- for this iteration's record and for this field in the fieldset
                create an outputText field. -->
    <apex:inputfield value="{!record[field]}" />

/The Controller will look like this:
   public with sharing class controller {

  // Can't use property syntax for component attributes so here's setters:
     public string sobj;
     public string fieldsetName;

   public void setSobj(string incomingSobj){
     this.sobj = incomingsObj;

public void setFieldsetName(string incomingFieldsetName){
   this.fieldsetName = incomingFieldsetName;

  public controller(){


  public List<Schema.FieldSetMember> getFields() {
        Schema.SObjectType t = Type.forName(this.sobj).getSObjectType();
      return t.contact.fieldSets.get(this.fieldsetName).getFields();

  • The reason to do this refactoring is get rid of a lot of code. Your solution brings addition pain - controller. Sep 17 '15 at 13:25
  • I dont see without writing code you would be able to optimize it .. Sep 17 '15 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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