function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tdeptdep 

Return values in a field to a drop-down on VF page

I am currently creating a page that is going to be used to track headcount in various locations.

 

They use the following control fields to select:

Location/State

Site

 

Is there a way to query the Location/Site field (currently a Text field) and convert it to a selectList or a drop-down type function I can use to filter queries?

 

The reason this is needed is that locations/states and sites are going to be added/removed frequently and it would be a huge headache to hard-code this as it will roll out to multiple clients.

 

I see some things on Describe but I am unsure on how to use it.

 

Any help would be greatly appreciated, thanks in advance!

 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

This link shows you how to do it.  Simply use queries to get the data instead of the hardcoded values they use.

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_selectList.htm|StartTopic=Content%2Fpages_compref_selectList.htm|SkinName=webhelp

All Answers

Damien_Damien_

This link shows you how to do it.  Simply use queries to get the data instead of the hardcoded values they use.

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_selectList.htm|StartTopic=Content%2Fpages_compref_selectList.htm|SkinName=webhelp

This was selected as the best answer
tdeptdep

Thanks!

 

For anyone that finds this later: 

 

Inside of: 

public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();

 

Add:

 

 

for (Object r : [select FIELDNAME from Object])
{
options.add(new selectOption(r.FIELDNAME.r.FIELDNAME));
}

 

 

and replace Object, FIELDNAME and change variable from r if needed.

 

To include a None as well, use the following: 

 

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();

options.add(new selectOption('', '- None -'));

for (Object r : [select FIELDNAME from Object])
{
options.add(new selectOption(r.FIELDNAME, r.FIELDNAME));
}

return options;
}

 

 

Happy coding!

SoozeeSoozee

Hi - this works great with one problem...

I have repetitive values in the drop down field that I want to omit.

 

I've tried to use the Group By clause, but I get this error:

 

Save error: Loop variable must be an SObject or list of AggregateResult

 

If I take the Group By out, I get duplicate items in the list.

 

Help?!

 

 

Damien_Damien_
public List<SelectOption> getItems()
{
  List<SelectOption> options = new List<SelectOption>();
  
  options.add(new selectOption('', '- None -'));
  Set<String> values = new Set<String>();
  for (Object r : [select FIELDNAME from Object])
  {
    values.add(r.FIELDNAME);
  }
  
  for (string val: values)
  {
    options.add(new selectOption(val, val));
  }
  
  return options;
}

 

tdeptdep

Hey, you need add a way to check against a list and then don't add if it's not unique.

 

Here is a snippet that works:

 

 public List<SelectOption> getSiteItems() {
            List<SelectOption> siteoptions = new List<SelectOption>();
            siteoptions.add(new selectOption(' ', '- None -'));
            
                 Set<String> sitestring = new Set<String>(); //use a string list to add and check for dupes
            for (Demand_Tracker__c r : [select Site__c from Demand_Tracker__c ORDER by Site__c ASC])
                    {
                    
                    If(!sitestring.contains(r.Site__c)) //this checks is the value is in the string and only adds if not present
                       {
                           sitestring.add(r.Site__c);
                           siteoptions.add(new selectOption(r.Site__c, r.Site__c));
                        }                    
                    }
            return siteoptions;
            }

 

Damien_Damien_

The code snippet i pasted above using a Set takes care of that.

SoozeeSoozee

Thanks!  This worked great!