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
SteveAtGFISteveAtGFI 

Change a select through a controller?

I have a request to create a mechanism that allows users of our knowledge base (using SalesForce knowledge) to automatically filter their selection based on URL. Was hoping to post here and get some advice.

 

The current functionality is as follows:

 

1. User goes to KB

2. User chooses product from select

3. Search filters based on select using controller / ajax

 

Desired Functionality:

 

User can go to KB in the traditional way and search normally OR:

 

1. User goes to kb.com/?product=MYPRODUCT

2. Controller gets the product variable

3. VisualForce sets the select to the chosen product based on the URL

 

What I've done:

 

1. Controller takes the product variable as a string

 

What I need to know how to do:

 

I'd like, somehow, to change the select based on the variable in my controller. 

 

This has to be something simple. Any ideas?

sfdcfoxsfdcfox

Use a "binding" in your page's query. For example, your controller might look like this:

 

public class pageController {
  public String searchTerm { get; set; }
  public List<Product2> searchResults { get; set; }

  public pageController() {
    if(ApexPages.currentpage().getParameters().get('product')!=null)
      searchTerm = ApexPages.currentpage().getParameters().get('product');
    searchResults = new List<Product2>();
    search();
  }

  public void search() {
    searchResults.clear();
    if(searchTerm==null) {
      searchTerm='';
      return;
} searchResults.addAll([select id,name,productcode from product2 where productcode = :searchTerm]); } }

The magic here is the ":searchTerm" in the query. This automatically binds the value in searchTerm into the query. It automatically escapes SOQL characters that could be used for SOQL injection. You could also add a "%" and use the LIKE operator instead of "=". Also, if searchTerm was bound to an apex:selectList, it would automatically update the list value to the selected term. You could also validate the input against available options and blank out the value if it is not a legal value.