Sunday 5 June 2011

Dynamic Binding : When Object Name is Dynamic Also

Hi,

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())
                    {
                        listObjectFields.add(fieldResult.getName());
                    }
                if(i == 5)
                    break;
                
                i++;
            }
                        
    }
}

Visulaforce Page

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


</apex:page>


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 : http://forceschool.blogspot.com/2011/06/dynamic-binding-using-field-sets.html

Regards




11 comments:

  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?

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

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

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

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

    ReplyDelete
  6. Hi Shashikant Sharma,,

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

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

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

    help me.....

    ReplyDelete
  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...

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  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();

    ReplyDelete

Tweet