+ Start a Discussion
Rakshana Cube84Rakshana Cube84 

How to pull all fields from an object dynamically to visualforce page

Hi All,

I have a scenario as showing all fields from an object in a visualforce page dynamically. That is I want to pull all the fields and put it in <apex:inputfield> component for creating a record from that page.
But If I am creating a field in that particular object which should be added automatically without changing the code.

Thanks,
Rakshana
Best Answer chosen by Rakshana Cube84
<Saket><Saket>
Hi Rakshana,

You have to use generic Sobject methodology to show all the fields into a vf page show that it can maintain dynamic nature.

So I am going to share u code snippet. With this uwill get a idea how to implement your own, and please igonre designing of my page because I am very poor in it :(

VisualForce Code
<apex:page Controller="CompleteFieldController">
    
    <apex:form >
       
       <apex:repeat value="{!allFieldName}" var="key"> 
           <apex:inputField value="{!sobj[key]}"/><br/>
       </apex:repeat>
        
    </apex:form>
    
    
</apex:page>

Apex Controller
 
public with sharing class CompleteFieldController {

    Public List<String> allFieldName{get;set;}
    Public Set<String> showName{get;set;}
    Public SObject sobj{get;set;}
    
    Public CompleteFieldController(){
        
        
        sobj = Schema.getGlobalDescribe().get('Account').newSObject();
        allFieldName = new List<String>();
        Map<String, Schema.SobjectField> allMap = 
        Schema.SobjectType.Account.fields.getMap();
        for(Schema.SobjectField field : allMap.values())
        {
            
            Schema.DescribeFieldResult dfr = field.getDescribe();
            if(dfr.isCreateable())
                allFieldName.add(dfr.getName());
        
        }
        
    }
    
    

}

For the demo purpose I did this for Account object but you can change it as per your requirement.


 

All Answers

Steven NsubugaSteven Nsubuga
Try using dynamic Apex, perhaps using a DescribeResult.
<Saket><Saket>
Hi Rakshana,

You have to use generic Sobject methodology to show all the fields into a vf page show that it can maintain dynamic nature.

So I am going to share u code snippet. With this uwill get a idea how to implement your own, and please igonre designing of my page because I am very poor in it :(

VisualForce Code
<apex:page Controller="CompleteFieldController">
    
    <apex:form >
       
       <apex:repeat value="{!allFieldName}" var="key"> 
           <apex:inputField value="{!sobj[key]}"/><br/>
       </apex:repeat>
        
    </apex:form>
    
    
</apex:page>

Apex Controller
 
public with sharing class CompleteFieldController {

    Public List<String> allFieldName{get;set;}
    Public Set<String> showName{get;set;}
    Public SObject sobj{get;set;}
    
    Public CompleteFieldController(){
        
        
        sobj = Schema.getGlobalDescribe().get('Account').newSObject();
        allFieldName = new List<String>();
        Map<String, Schema.SobjectField> allMap = 
        Schema.SobjectType.Account.fields.getMap();
        for(Schema.SobjectField field : allMap.values())
        {
            
            Schema.DescribeFieldResult dfr = field.getDescribe();
            if(dfr.isCreateable())
                allFieldName.add(dfr.getName());
        
        }
        
    }
    
    

}

For the demo purpose I did this for Account object but you can change it as per your requirement.


 
This was selected as the best answer
<Saket><Saket>
Hi Rakshana,

Please Let us know that your problem is solved or if your problem is solved by above answers then please mark that answer as a best answer.

Thanks
Saket Sharma
Rakshana Cube84Rakshana Cube84
Saket Sharma - I am happy with the code, now I can see all the fields are putting in the place. Thank you for the details.

Thanks,
Rakshana