• Eric Whipple
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

I have a selectlist value that should be passed as a parameter to my controller (using Javascript) in order to rerender the search results of a dynamic SOQL query. I have looked through several forum posts on this site as well as external web resources but haven't had any luck so I thought I'd post my code and maybe someone will see what I've missed; thanks so much!

 

VF Page:

 

<apex:page standardController="Janela_de_Acesso__c" extensions="JDABP" sidebar="false">
<apex:sectionHeader title="Janela de Acesso"/>
<apex:form >
<div align="center">
<apex:messages styleClass="errorMsg"/>
<apex:commandButton action="{!Save}" value="Save"/>
<apex:commandButton action="{!SaveAndNew}" value="Save & New"/>
<apex:commandButton action="{!Cancel}" value="Cancel"/>
</div>
<apex:pageBlock >
<apex:pageBlockSection title="Informações" columns="2">
<apex:inputField required="true" value="{!Janela_de_Acesso__c.Name}" />
<apex:inputField required="true" value="{!Janela_de_Acesso__c.Data_Inicial__c}"/>
<apex:inputField required="true" value="{!Janela_de_Acesso__c.Status__c}"/>
<apex:inputField required="true" value="{!Janela_de_Acesso__c.Data_Final__c}"/>
</apex:pageBlockSection>
<!--document.getElementById("pesquisa").options[document.getElementById("pesquisa").selectedIndex].value
<script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById('searchBox').value,
          document.getElementById('pesquisa').options[document.getElementById('pesquisa').selectedIndex].value);
      }
</script>-->
 
<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="Available">
    <apex:param name="searchB" value="" />
    <apex:param name="pesq" value="" />
</apex:actionFunction>

<apex:pageBlockSection title="Membros da Janela de Acesso" columns="1" id="Available_BPs">

<apex:panelGrid columns="2" cellpadding="5px">
<apex:pageBlockSectionItem labelStyle="vertical-align: middle" >
<apex:outputLabel value="Pesquisa:" for="SearchList"/>
<apex:selectList title="Pesquisa" size="1" id="pesquisa" onchange="searchServer(
          document.getElementById('searchBox').value,
          this.options[this.selectedIndex].value);">
<apex:selectOptions value="{!items}" id="pesquisa2"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem labelStyle="vertical-align: middle">
<apex:outputLabel value="for:" for="SearchBox"/>
<apex:inputText id="SearchBox" onkeyup="searchServer(
          document.getElementById('searchBox').value,
          document.getElementById('pesquisa').options[document.getElementById('pesquisa').selectedIndex].value);"/>
</apex:pageBlockSectionItem>
</apex:panelGrid>
<apex:panelGrid columns="2">
<apex:outputLabel value="Membros Disponíveis" style="font-weight:bold"/>
<apex:outputLabel value="Membros Selecionados" style="font-weight:bold"/>
</apex:panelGrid>
<apex:panelGrid columns="3">
<apex:outputPanel id="Available">
<apex:selectList title="Membros Disponíveis" size="14" id="Available2">
<apex:selectOptions value="{!searchResults}" />
</apex:selectList>
</apex:outputPanel>
<apex:selectList title="Membros Selecionados" size="14" id="Selected"/>
</apex:panelGrid>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 

Controller:

 

public class JDABP {    
    
    // the soql without the order and limit
    private String soql {get;set;}
    // the collection of contacts to display
    public List<sObject> results {get;set;}
    public String searchObject;
    public Janela_de_Acesso__c j;
    public List<SelectOption> searchResult = new List<SelectOption>();
    public String sObjectDesc = 'Business Plans';

    
    //Constructor to apply fields
    public JDABP(ApexPages.StandardController controller) {
        j = (Janela_de_Acesso__c) controller.getRecord();
        j.Name='|';
        j.Status__c='Ativo';
        j.Data_Inicial__c=System.now();
        j.Data_Final__c=System.today()+30;        
        //j.RecordTypeId = Label.Janela_de_Acesso_BP_RecordType;
        searchObject = 'Business_Plan__c';
        soql = 'select Id, Name from ' + searchObject + ' limit 100';
        runQuery();

    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('Business Plans','Business Plans'));
            options.add(new SelectOption('Perfis','Perfis'));
            options.add(new SelectOption('Usuários','Usuários'));

        return options;
    }     
    
    public List<SelectOption> getSearchResults() {
        return searchResult;
    }
    
    // runs the actual query
  public void runQuery() {    
    try {
      results = Database.query(soql);
      searchResult.clear(); 
      for (sObject r : results){
          If (sObjectDesc=='Business Plans'){
              Business_Plan__c BPr = (Business_Plan__c) r;
              searchResult.add(new SelectOption(BPr.name,BPr.name));
          }
          Else If (sObjectDesc=='Perfis'){
              Profile Pr = (Profile) r;
              searchResult.add(new SelectOption(Pr.Name,Pr.Name));
          }
          Else {
              User Ur = (User) r;
              searchResult.add(new SelectOption(Ur.Name,Ur.Name));
          }         
      }
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, soql+' '+e.getMessage()));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String searchText = Apexpages.currentPage().getParameters().get('searchB');
    sObjectDesc = Apexpages.currentPage().getParameters().get('pesq');
    If (sObjectDesc=='Business Plans'){
    searchObject='Business_Plan__c';
    }
    Else If (sObjectDesc=='Perfis'){
    searchObject='Profile';
    }
    Else {
    searchObject='User';
    }
 
    soql = 'select Id, Name from ' + searchObject + ' limit 100';
    if (!searchText.equals(''))
      soql += ' and Name LIKE \''+String.escapeSingleQuotes(searchText)+'%\'';
    
 
    // run the query again
    runQuery();
 
    return null;
  }

        
    //Override Save & New Functionality
    public PageReference SaveAndNew() {
        try{
            insert j;
        } catch (DMLException e) {return null;}
        PageReference pr = new PageReference('/apex/JDABP');
        pr.setRedirect(true);
        return pr;
    }

}

 

Thanks again!

I have few problems on <apex:detail>.

 

  1. Can we use <apex:detail> for custom Objects?
  2. When I use <apex:detail>, do I need to create any kind of page?

Plz anyone clarify about <apex:detail> .