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
Medhanie HabteMedhanie Habte 

Enabling Apex Classes to search for more fields

Hi all, I've been working on this visualforce lookup page, to successful results. I've modified to to specific work with the contact object with great success and a 76% pass rate in apex tests.

The page is modeled after this blog and is for a custom app/object we are building
http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/

One of the things we are looking to do is include additional columns for contact fields specific to a search result. I have been able to successfully replicate this result with the contact name, appearing in two tables.

User-added image


However, I am trying to figure out a way to enable results for additional fields within the contact object, allowing the second column to reveal other fields such as address, account etc. Ideally, I envision that there would be columns for account, mailing street, phone and email.

I'd love to know more about how to achieve enhancing my Soql results.

Currently, here is how my code is configured.

APEX CLASS

public class ContactLookupControl {

  public Contact contact {get;set;} // new contact to create
  public List<Contact> results{get;set;} // search results
  public string searchString{get;set;} // search keyword

  public ContactLookupControl() {
    contact = new Contact();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }

  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }

  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 

  // run the search and return the records found. 
  private List<Contact> performSearch(string searchString) {

    String soql = 'select id, name from contact';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 

  }

  // save the new contact record
  public PageReference saveContact() {
    insert contact;
    // reset the contact
    contact = new Contact();
    return null;
  }

  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }

  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }



VF PAGE

<apex:page controller="ContactLookupControl"
  title="Search"
  showHeader="false"
  sideBar="false"
  tabStyle="Contact"
  id="pg">
  <apex:form >
  <apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
  <apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">
    <!-- SEARCH TAB -->
      <apex:tab label="Search" name="tab1" id="tabOne"> 
      <apex:actionRegion > 
      <apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
        <apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
        <apex:inputText id="txtSearch" value="{!searchString}" />
          <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
      </apex:outputPanel>
   
       <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
        <apex:pageBlock id="searchResults">
          <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
            <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Name</apex:outputPanel>
              </apex:facet>
               <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
            </apex:column>
              <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Name</apex:outputPanel>
              </apex:facet>
               <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
       </apex:outputPanel>
      </apex:actionRegion>
    </apex:tab>
    <!-- NEW CONTACT TAB -->
    <apex:tab label="New Contact" name="tab2" id="tabTwo">
        <apex:pageBlock id="newContact" title="New Contact" >
          <apex:pageBlockButtons >
            <apex:commandButton action="{!saveContact}" value="Save"/>
          </apex:pageBlockButtons>
          <apex:pageMessages />
          <apex:pageBlockSection columns="2">
              <apex:inputField value="{!Contact.FirstName}"/>
              <apex:inputField value="{!Contact.LastName}"/>
              <apex:inputField value="{!Contact.AccountId}"/>
              <apex:inputField value="{!Contact.MailingStreet}"/>
              <apex:inputField value="{!Contact.MailingCity}"/>
              <apex:inputField value="{!Contact.MailingState}"/>
           <apex:inputField value="{!Contact.MailingPostalCode}"/>
           <apex:inputField value="{!Contact.MailingCountry}"/>
           <apex:inputField value="{!Contact.Phone}"/>
           <apex:inputField value="{!Contact.Email}"/>
          </apex:pageBlockSection>
        </apex:pageBlock>   
    </apex:tab>
  </apex:tabPanel>
  </apex:outputPanel>
  </apex:form>
</apex:page>

Best Answer chosen by Medhanie Habte
KaranrajKaranraj
Medhanie Habte - Update the soql query string with column names you want to display in the visualforce page. Then update your visualforce page to display the value in your page.

Apex
public class ContactLookupControl {

  public Contact contact {get;set;} // new contact to create
  public List<Contact> results{get;set;} // search results
  public string searchString{get;set;} // search keyword

  public ContactLookupControl() {
    contact = new Contact();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }

  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }

  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 

  // run the search and return the records found. 
  private List<Contact> performSearch(string searchString) {

    String soql = 'select id, name,account.Name,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Email from contact';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 

  }

  // save the new contact record
  public PageReference saveContact() {
    insert contact;
    // reset the contact
    contact = new Contact();
    return null;
  }

  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }

  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }


Visualforce
<apex:page controller="ContactLookupControl"
  title="Search"
  showHeader="false"
  sideBar="false"
  tabStyle="Contact"
  id="pg">
  <apex:form >
  <apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
  <apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">
    <!-- SEARCH TAB -->
      <apex:tab label="Search" name="tab1" id="tabOne"> 
      <apex:actionRegion > 
      <apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
        <apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
        <apex:inputText id="txtSearch" value="{!searchString}" />
          <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
      </apex:outputPanel>
   
       <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
        <apex:pageBlock id="searchResults">
          <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
            <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Name</apex:outputPanel>
              </apex:facet>
               <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
            </apex:column>
              <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Name</apex:outputPanel>
              </apex:facet>
               <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
            </apex:column>
        <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Account Name</apex:outputPanel>
              </apex:facet>
               {!a.Account.Name}   
            </apex:column>
        <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Street</apex:outputPanel>
              </apex:facet>
               {!a.MailingStreet}   
            </apex:column>
  <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >City</apex:outputPanel>
              </apex:facet>
               {!a.MailingCity}   
            </apex:column>
 <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >State</apex:outputPanel>
              </apex:facet>
               {!a.MailingState}   
            </apex:column>
 <apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Postal Code</apex:outputPanel>
              </apex:facet>
               {!a.MailingPostalCode}   
            </apex:column>
<apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Phone</apex:outputPanel>
              </apex:facet>
               {!a.Phone}   
            </apex:column>
<apex:column >
              <apex:facet name="header">
                <apex:outputPanel >Email</apex:outputPanel>
              </apex:facet>
               {!a.Email}   
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
       </apex:outputPanel>
      </apex:actionRegion>
    </apex:tab>
    <!-- NEW CONTACT TAB -->
    <apex:tab label="New Contact" name="tab2" id="tabTwo">
        <apex:pageBlock id="newContact" title="New Contact" >
          <apex:pageBlockButtons >
            <apex:commandButton action="{!saveContact}" value="Save"/>
          </apex:pageBlockButtons>
          <apex:pageMessages />
          <apex:pageBlockSection columns="2">
              <apex:inputField value="{!Contact.FirstName}"/>
              <apex:inputField value="{!Contact.LastName}"/>
              <apex:inputField value="{!Contact.AccountId}"/>
              <apex:inputField value="{!Contact.MailingStreet}"/>
              <apex:inputField value="{!Contact.MailingCity}"/>
              <apex:inputField value="{!Contact.MailingState}"/>
           <apex:inputField value="{!Contact.MailingPostalCode}"/>
           <apex:inputField value="{!Contact.MailingCountry}"/>
           <apex:inputField value="{!Contact.Phone}"/>
           <apex:inputField value="{!Contact.Email}"/>
          </apex:pageBlockSection>
        </apex:pageBlock>   
    </apex:tab>
  </apex:tabPanel>
  </apex:outputPanel>
  </apex:form>
</apex:page>