Sunday, 5 June 2011

Dynamic Binding : When Object Name is Dynamic Also


In earlier post we talked about dynamic binding with field set in this object name was static and fields were coming dynamically from Field Sets.Now we will talk about other way when both Object and Fields are dynamic.

Now we will take examples where object name and fields both are dynamic and fields are not coming from Field Sets either.

Controller Class
public class DynamicBinding {
    public sObject sObjectToBind {get;set;}
    public List<String> listObjectFields {get;set;}
    public DynamicBinding()
        listObjectFields =  new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        //In this example I have hard coded the object name
        Schema.sObjectType sObjType = globalDescription.get('Account');
        sObjectToBind = sObjType.newSObject();
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();
        Integer i = 0;
        for(Schema.SObjectField field : mapFieldList.values())
                Schema.DescribeFieldResult fieldResult = field.getDescribe();
                if(fieldResult.isAccessible() && fieldResult.isUpdateable())
                if(i == 5)

Visulaforce Page

<apex:page controller="DynamicBinding">
       <apex:pageBlock title="Without Field Set Use">
       <apex:pageBlockSection title="Dynamic Object">
           <apex:repeat value="{!listObjectFields}" var="fieldAPIName">
               <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>


So in above example I used an sObject instance and showed 5 fields only of that Object type. In above example I used Account object you can decide any object pass the name from any parameter or get as input from User using Picklist.

Related Post :



  1. thanks Sir it is really a worthy approach for getting fields of an Object(even it is dynamic) without using Field Set.
    max. how many fields of that object we can show here?

  2. @Sandeep
    Any number of fields you can bind I used 5 just for example.

  3. Yes Its really Good Example.
    Could you please Implement Save() for this page.

  4. Thank you very much, it's just what I was looking for.

  5. Hi Shashikant Sharma,
    This is very help ful to my task.Could you please Implement Save() for this page.

  6. Hi Shashikant Sharma,,

    please help me how to give save() functionality of above without using fieldset related dynamic object .

    please help me................

  7. Hi sreekanth,
    I want to save functionality of without using fieldset functionality

    help me.....

  8. Hi sreekanth,
    Using above code i displayed all fields dynamically without using fieldset in an visualforce page, On my page More than one multiselectpicklists are there.I want to how to display all these multiselectpicklists values as checkboxes Dynamically?

    help me...

  9. This comment has been removed by the author.

  10. Hi.
    Im trying to implement a similar solution to input the mandatory fields of an sobject. However, the values that I enter in the form via inputField is not getting set to the Sobject.

    campaignToInsert is an Sobject initialised as campaignToInsert = Schema.getGlobalDescribe().get('Campaign').newSObject();