1

I have a an object that defines attributes and the type of attribute; for instance Nme (String), LastSeenDate__c (Date), Age (Number), Countries (Picklist), etc.

Based on this information, I want to build a Visualforce Page where a Date field is displayed with a date picker, a picklist field is displayed as selectOptions, and so on.

To make things more interesting, this should be a list. This means that the 1st row will show "Name" and accept "STRING" from user, the second row may show "LastSeenDate" and show date picker.

Is it possible to achieve something like this?

6
  • 2
    Visualforce provides the apex:inputField dedicated to this. Mar 20, 2017 at 14:11
  • 1
    As Martin Lenzer notes, <apex:inputField> is the tool for the job here. Is there a particular reason why you don't want to use that? If you're looking for a way to add a collection of sObject fields to a Visualforce page without typing <apex:inputField> over and over, you should take a look at using fieldsets
    – Derek F
    Mar 20, 2017 at 14:24
  • How do I use inputField? I could not find a way to render dropdown list if the type of the field is dropdown. I could use type=data etc. Can you please provide me some pointers?
    – vetal_king
    Mar 20, 2017 at 14:29
  • There's an example in the documentation I provided. Mar 20, 2017 at 14:42
  • @vetal_king the idea here is that you'd use an SObject (like Account) that has field(s) of the appropriate type (from your example, Text, Date, Number, and Picklist, respectively) instead of using your custom object (which is the 'hard way' of accomplishing what you're looking to do).
    – Derek F
    Mar 20, 2017 at 15:14

1 Answer 1

2

As mentioned in the comments, this is exactly how <apex:inputField> works.

Markup

<apex:page standardController="MyObject__c">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:inputField value="{!MyObject__c.Name}" />
                <apex:inputField value="{!MyObject__c.Last_Seen_Date__c}" />
                <apex:inputField value="{!MyObject__c.Age__c}" />
                <apex:inputField value="{!MyObject__c.Country__c}" />
                <apex:inputField value="{!MyObject__c.Countries__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Screenshots

Date Picker:

Date Picker

Picklist:

Picklist

Field Sets

You can also very easily move these fields to a configurable list using a Field Set. You still won't even need Apex.

<apex:page standardController="MyObject__c">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:repeat value="{!$ObjectType.MyObject__c.FieldSets.Demo}" var="field">
                    <apex:inputField value="{!MyObject__c[field]}" />
                </apex:repeat>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .