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
HareHare 

Search functionality on multiple objects using apex and vfpage

Hi All,

My requirment is suppose i have contact,account,lead,oppertunity  object in vf i have one text box and one scarch button if am giveing any text and clicking on search button it shoud search that text on contact,account,lead,oppertunity objects and display data. i know how to do it on ome object like Lead how to implement on multiple objects using apex and vf page .

below is my code for single object search functionality :


vf page
------------------------------------------------------------------------------------
<apex:page controller="Leadpage2">
   <apex:form >
   <apex:pageMessage severity="error" strength="2"  summary="Error:" detail="{!errorMessage}"  rendered="{!errorMessage != null}"/>
   <table>
   <tr>
   <td>Lead: </td>
   <td><apex:inputtext value="{!name}" required="true"/></td>
   <td></td>
   <td><apex:commandButton action="{!search}" value="Go"/>
   </td>
   </tr>
   </table>
   <apex:pageBlock rendered="{!name != null}" >
   <apex:pageBlockSection title="Lead Details" rendered="{!errorMessage == null}">
   <apex:pageBlockTable value="{!records}" var="rec">
   <apex:column value="{!rec.Name}" headerValue="Name"/>
   <apex:column value="{!rec.Email}" headerValue="Email"/>
   <apex:column value="{!rec.Company}" headerValue="Company"/>
   </apex:pageBlockTable>
   </apex:pageBlockSection>
   </apex:pageBlock>
   </apex:form>
</apex:page>
----------------------------------------------------------------------------------------------------------
apex page
---------------------------------------------------------------------------------------
public class Leadpage2 {
public String name{ get ; set;}
List<Lead> records;
List<Lead> templi;
public String msg {get; set; }
public transient String errorMessage { get; set; }
String s { get; set; } 
    public List<Lead> getrecords(){
      return records;
      }
      public Leadpage2(){
       records = new List<Lead>();
    }
    public PageReference search() {
      s = '%' + name + '%';
       if(name.length()==0){
          errorMessage = 'Please enter a Lead Name';
         }
      records  =[select Name,Email,Company from Lead  where name like:s OR Email like:s OR Company like:s ];
       if(records !=null && records.size() ==0){
           errorMessage = 'LEAD NOT fOUND !';
         }
     return null;
   }
}

-----------------------------------------------------

any ideas appreciated........

Thanks,

Hareesh Goud N
ashish.kumar171.3969654430835393E12ashish.kumar171.3969654430835393E12
Hi Hareesh , 

You are need to work on this using the dynamic query. For further reference you can refer https://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_globals_objecttype.htm.

I suppose this helps you out to resolve you query.
VictorFelisbinoVictorFelisbino
You will have to create a custom class to return the data in just one list:
public class leadsSearched{
public String name{get;set;}
public String email{get;set;}
public String Company{get;set;}
public String Sobject{get;set;}
}

Than what you will need to do is search all objects individually and add the results to the list
something like:

List<leadsSearched> leadsList = new List<leadsSearched>();
for(Lead l :[select Name,Email,Company from Lead  where name like:s OR Email like:s OR Company like:s ]){
leadsSearched ls = new leadsSearched();
ls.name = l.Name;
ls.email = l.Email;
ls.Company = l.Company;
ls.Sobject = 'Lead';
leadsList.add(ls);
}
for(Contact c :[select Name,Email,Company from Contact where name like:s OR Email like:s OR Company like:s ]){
leadsSearched ls = new leadsSearched();
ls.name = c.Name;
ls.email = c.Email;
ls.Company = c.Company;
ls.Sobject = 'Contact';
leadsList.add(ls);
}
for(Account a :[select Name,Email,Company from Account where name like:s OR Email like:s OR Company like:s ]){
leadsSearched ls = new leadsSearched();
ls.name = a.Name;
ls.email = a.Email;
ls.Company = a.Company;
ls.Sobject = 'Account';
leadsList.add(ls);
}

Than in your visualforce you would show the leadsSearched in you pageBlockTable

Let me know if you have any quetions