Wednesday, 8 February 2012

SOSL on Multiple Object(searching records on multiple records)


======================
Controller Code
========================
public class theController1 {
 String searchText;
 List<Account> accounts=new List<Account>();
 List<Contact> contacts= new List<Contact>();
 List<opportunity> opportunities=new List<opportunity>();
 List<Lead> leads=new List<Lead>();

 public String getSearchText() {
      return searchText;
   }

   public void setSearchText(String s) {
      searchText = s;
   }

   public List<Lead> getResults() {
   
      return leads ;
   }
   public List<Account> getAccounts(){
       return accounts;
   }
   public List<Contact> getContacts(){
       return contacts;
 
   }
   public List<Opportunity> getOpportunities(){
 
        return opportunities;
 
   }
   public PageReference doSearch() {
   
        if(searchText!=null && searchText!=''){
            List<List<SObject>> searchList = [FIND :searchText+'*' IN All Fields RETURNING Account (id,name,industry), Contact(id,name,email,firstname,lastname),Opportunity(id,name,stagename,closedate),lead(id,name,email,phone)];
            system.debug('searchlist===========>'+searchlist);
            accounts = ((List<Account>)searchList[0]);
            contacts = ((List<Contact>)searchList[1]);
            opportunities=((List<Opportunity>)searchList[2]);
            leads=((List<lead>)searchList[3]);  
       }
      return null;
        
   }

}
=========================
Visualforce page code
=========================

<apex:page controller="theController1">
<style>
.tertiaryPalette{
 background-color:#1797C0 !important;

}
.multiforce{
     background-color:#336600 !important;
}
.headerSearchLeftRoundedCorner{
background-color:#000000 !important;

}
.headerSearchContainer{

background-color:#000000 !important;
}

</style>
   <apex:form >
      <apex:pageBlock mode="edit" id="block">
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
               <apex:panelGroup >
                   <apex:inputText id="searchText" value="{!searchText}"/>
                  <apex:commandButton value="Go!" action="{!doSearch}" rerender="block" status="status"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="Leads" id="results" columns="1">
           <apex:pageBlockTable value="{!results}" var="l" rendered="{!NOT(ISNULL(results))}">
              <apex:column value="{!l.name}"/>
              <apex:column value="{!l.email}"/>
              <apex:column value="{!l.phone}"/>
           </apex:pageBlockTable>
            </apex:pageBlockSection>
        <apex:pageBlocksection title="Accounts" id="accounts" columns="1">
            <apex:pageBlockTable value="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
              <apex:column value="{!a.name}"/>
              <apex:column value="{!a.industry}"/>
            </apex:pageBlockTable>
         </apex:pageBlocksection>
          <apex:pageBlocksection title="Opportunities" id="Opportunities" columns="1">
             <apex:pageBlockTable value="{!Opportunities}" var="o" rendered="{!NOT(ISNULL(Opportunities))}">
              <apex:column value="{!o.name}"/>
              <apex:column value="{!o.stagename}"/>
              <apex:column value="{!o.closedate}"/>
            </apex:pageBlockTable>
         </apex:pageBlocksection>
         <apex:pageblocksection title="Contacts" id="Contacts" columns="1">
            <apex:pageBlocktable value="{!Contacts}" var="c" rendered="{!NOT(ISNULL(contacts))}">
                     <apex:column value="{!c.name}"/>
                     <apex:column value="{!c.firstname}"/>
                     <apex:column value="{!c.email}"/>
             </apex:pageBlocktable>
         </apex:pageblocksection>
     </apex:pageBlock>
   </apex:form>
</apex:page>
======================
Output
===================


No comments:

Post a Comment

Get the Developer Name for Record Types without SOQL query

Hi, Previously, the developer name was accessible only via SOQL on the RecordType SObject, and not via describe information. Now you can ...