Dear awesome readers,
If you have already played with sObjects and Visualforce, you have surely gone through the below link. If you havn’t please do. Its simple and sets the tone for the revelation in this post.
The developer guide talks precisely what we need to display an sObject on VF. i.e. :
While in apex we do:
sobject s1 = Account.sObjectType().getDescribe().newSobject(); s1.put('name','awesome reader');
In visualforce we will do the following:
Now, what happens if we are to deal with an sObject List? Lets experiment:
List<sobject> s1 = new List<sobject>(); sobject s1 = Account.sObjectType().getDescribe().newSobject();
In visualforce the following makes logical sense:
However it throws a compilation error!!!
hmmm… So what is the right way? While there are many my personal favorite is to use apex:repeat in the following manner:
<apex:repeat value="s1" var="sobj"> <apex:inputField value="sobj['name']"/> </apex:repeat>
Eureka!! It works!