Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

In my visualforce page I am showing various fields from Account page where user can input values and search for matching accounts. Is it possible to create these filters dynamically, may be using describe sobject query. So that all fields come automatically on vf page

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Depending on what you want to do, there are a couple of solutions.

If you want to display all Account fields I would suggest using getDescribe().fields.getMap().keySet() and then iterating over the result in an apex:repeat to build you page.

public with sharing class SearchPageController
{
    public final Account anAccount { get; set; }
    public List<String> searchFields { get; private set; }


    public SearchPageController()
    {
        anAccount = new Account();
        searchFields = new List<String>(Account.getSObjectType().getDescribe().fields.getMap().keySet());
    }
}

Then on your page (you'll want to adjust the 'look and feel', as this example doesn't look great!):

<apex:page controller="SearchPageController">
    <apex:form>
        <apex:repeat value="{!searchFields}" var="field">
            <apex:outputText value="{!field}"/>
            <apex:inputField value="{!anAccount[field]}"/>
            <br/>
        </apex:repeat>
    </apex:form>
</apex:page>

You can then use the values stored within the Account object inside your controller to build your dynamic SOQL query for your search.


If you only want to display some of the fields on Account then I would suggest using Field Sets, in which case you need to change your controller to something like the following:

public with sharing class SearchPageController
{
    public final Account anAccount { get; set; }
    public List<String> searchFields { get; private set; }


    public SearchPageController()
    {
        anAccount = new Account();
        searchFields = new List<String>();

        for(Schema.FieldSetMember f : SObjectType.Account.FieldSets.YourFieldSet.getFields()) 
        {
            searchFields.add(f.getFieldPath());
        }
    }
}

OR, you can change your apex:repeat to use the Field Set directly.

<apex:page controller="SearchPageController">
    <apex:form>
        <apex:repeat value="{!$ObjectType.Account.FieldSets.YourFieldSet}" var="field">
            <apex:outputText value="{!field.Label}"/>
            <apex:inputField value="{!anAccount[field]}"/>
            <br/>
        </apex:repeat>
    </apex:form>
</apex:page>

Again, you can then use the values stored within the Account object inside your controller to build your dynamic SOQL query (or whatever you're using) for your search.

share|improve this answer
    
Thanks. This is very helpful –  vishesh Apr 7 at 12:27
    
So I have the Account object in the controller which has the values given by user. How do I make the dynamic soql query ? –  vishesh Apr 21 at 20:12
    
@vishesh What problem are you having? Do you not know how to use dyanmic SOQL? Or is your problem actually creative the query? You need to build the query as a String object and then use Database.query(...) to execute it. –  Alex Tennant Apr 22 at 8:54
    
I found the answer to it here salesforce.stackexchange.com/questions/33276/… .Thanks for the help –  vishesh Apr 22 at 13:43
add comment

Have you looked into Dynamic Visualforce Components. These will allow you to construct page components based on many varying factors, one of which could be in response to use input.

http://wiki.developerforce.com/page/Dynamic_Visualforce_Components

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.