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
Dchris222Dchris222 

Search on two objects and display the results in separate tables.

I am trying to display two object on the same page in tables and then have search bars that search across both objects (Oppertunites and Tradeins) on all fields being shown.

 

Currently, I have the page populate all Oppertunities and TradeIns upon page load and display them in seperate tables (This is working just fine, but I plan on disabling b/c it is useless).

 

I first wanted to tackle geting the Tradein search to work so I then have textboxes that will allow the user to search on each field. I started with TradeIns search and I can't figure out what i am doing wrong. I would like the TradeIn table to update upon a user changing the search fields and have added the action to the fields, but it is not working. I then tried a button that runs the action, but that to is not working. I am not recieving any errors so it is really hard to diagnose what is wrong.

 

 

I am trying to do somthing very similiar to what has been done on this site, but with two objects.

 

Any assistance would be really appreciated.

 

Chris

 

Here is my code

 

Controller for TradeIn Component


public with sharing class TradeInController2 {
                
    // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List<Trade_in__c> TradeIn{get;set;}
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'name'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public TradeInController2() {
    soql = 'select name, Deinstall_Date__c, Status__c, Modality2__c, Modality__c from Trade_in__c';
    runQuery();
  }
 
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
      TradeIn = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String name1= Apexpages.currentPage().getParameters().get('name');
    String Ddate1= Apexpages.currentPage().getParameters().get('Ddate');
    String EquipmentState1 = Apexpages.currentPage().getParameters().get('EquipmentState');
    String IncomingModality1= Apexpages.currentPage().getParameters().get('IncomingModality');
    String ExternalModality1= Apexpages.currentPage().getParameters().get('ExternalModality');
 
    soql = 'select name, Deinstall_Date__c, Status__c, Modality2__c,  Modality__c from Trade_in__c';
    if (!name1.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name1)+'%\'';
    if (!Ddate1.equals(''))
      soql += ' and Deinstall_Date__c LIKE \''+String.escapeSingleQuotes(Ddate1)+'%\'';
    if (!EquipmentState1.equals(''))
      soql += ' and Status__c LIKE \''+String.escapeSingleQuotes(EquipmentState1)+'%\'';
    if (!IncomingModality1.equals(''))
      soql += ' and Modality2__c LIKE \''+String.escapeSingleQuotes(IncomingModality1)+'%\'';
    if (!ExternalModality1.equals(''))
      soql += ' and Modality__c LIKE \''+String.escapeSingleQuotes(ExternalModality1)+'%\'';

    // run the query again
    runQuery();
 
    return null;
  }
}



Component


<apex:component controller="TradeInController2">

        <apex:form >
        <apex:pageMessages id="errors" />

        <apex:pageBlock mode="edit" id="criteria">

        <script type="text/javascript">
          function doSearch() {
            searchServer(
          document.getElementById("name1").value,
          document.getElementById("Ddate1").value,
          document.getElementById("EquipmentState1 ").value,
          document.getElementById("IncomingModality1").value,
          document.getElementById("ExternalModality1").value
          );
      }
      </script>
 
      <apex:actionFunction id="Test" name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="name1" value="" />
          <apex:param name="Ddate1" value="" />
          <apex:param name="EquipmentState1 " value="" />
          <apex:param name="IncomingModality1" value="" />
          <apex:param name="ExternalModality1" value="" />

      </apex:actionFunction>
      
      <apex:commandButton value="Search" onclick="{function doSearch()}"/>
      
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Trade-In Number<br/>
        <input type="text" id="name1" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">De-Install Date<br/>
        <input type="text" id="Ddate1" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Equipment State<br/>
        <input type="text" id="EquipmentState1" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Incoming Modality<br/>
        <input type="text" id="IncomingModality1" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">External Modality<br/>
        <input type="text" id="ExternalModality1" onkeyup="doSearch();"/>
        </td>
      </tr>

      </table>
      </apex:pageBlock>
      
      
      <apex:pageBlock mode="edit" id="results">
      
       <apex:pageBlockTable value="{!TradeIn}" var="trade">
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Trade-In Number" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!trade.name}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="De-Install Date" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Deinstall_Date__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!trade.Deinstall_Date__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Equipment State" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Status__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!trade.Status__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Incoming Modality" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Modality2__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!trade.Modality2__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="External Modality" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Modality__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!trade.Modality__c}"/>
            </apex:column>
 
        </apex:pageBlockTable>
     
     <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}"/>           
     </apex:pageBlock>    

     
     </apex:pageBlock>
     
     
        </apex:form>
</apex:component>




Salesforce Page


<apex:page standardController="Account">

    <apex:pageBlock title="Search">
   
        <apex:dataTable value="{!account.Opportunities}" var="opportunity" cellPadding="4" border="1">
              <apex:column >
               <apex:facet name="header">Opportunity Name</apex:facet>
                {!opportunity.Name}
              </apex:column>
              <apex:column >
               <apex:facet name="header">Close Date</apex:facet>
                {!opportunity.CloseDate}
              </apex:column>
              <apex:column >
                <apex:facet name="header">Equipment state</apex:facet>
                  {!opportunity.Equipment_State__c}
              </apex:column>
              <apex:column >
                <apex:facet name="header">Incoming Equipment</apex:facet>
                 <apex:outputField value="{!opportunity.Incoming_Equipment__c}"/>
              </apex:column>
               <apex:column >
                <apex:facet name="header">Outgoing Equipment</apex:facet>
                 <apex:outputField value="{!opportunity.Outgoing_Equipment__c}"/>
              </apex:column>
        </apex:dataTable>
     </apex:pageBlock>
    
     
      <apex:pageblock title="Trade-Ins">
           <c:TradeInComponent />
     </apex:pageBlock> -->
     
     
   <apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="false" title="false"/>
</apex:page>