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 like to add a field to a VF page when a field is added to a object by admin with out re coding the VF page. Is their any way to add fields to VF page automatically when a field get created on the object.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

You can create Fieldset on that object and then iterate that field set on your VF page.

You can iterate field set like this on VF page -

               <apex:repeat value="{!ACTFields}" var="f" >                             
                    <apex:inputField value="{!testDetails[f.fieldPath]}" required="{!OR(f.required,f.dbrequired)}" />              
               </apex:repeat>

and in controller you can do like this -

public Test_Score__c testDetails {get;set;}

public List<schema.fieldsetmember> getACTFields(){      
    return getFields('Test_Score__c', 'ACT');
}

public static List<schema.fieldsetmember> getFields(String objectName, String fieldSetName) {  
        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); 
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(objectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();            
        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);        
        return fieldSetObj.getFields(); 
} 
share|improve this answer

Yes! Fieldsets were created to meet exactly this requirement. From the docs:

You can use dynamic bindings to display field sets on your Visualforce pages. A field set is a grouping of fields. For example, you could have a field set that contains fields describing a user's first name, middle name, last name, and business title. If the page is added to a managed package, administrators can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code.

share|improve this answer

Two solutions. The first, proposed by grigriforce already, is to use Fieldsets. They have the advantage that an admin gets to specify which fields they want, and where, in the locations that use that fieldset. They carry the disadvantage that the admin must do this, so in the case where you always want new fields to show up automatically, it's more work for no gain.

The second alternative is to use the somewhat under-the-radar feature of dynamic field bindings in VF. You use the Apex Describe API to get a list of fields (in this case, all editable fields) for an object, then you dynamically bind them in your page. Example controller properties:

public List<String> myFields {get;set;}
public Custom_Object__c myObject {get;set;}

Example VF page:

<apex:repeat var="fieldName" value="{!myFields}">
    <apex:inputField value="{!myObject[fieldName]}" />
</apex:repeat>
share|improve this answer

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.