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
Sujan Kumar ReddySujan Kumar Reddy 

How to build Dynamic SOSL queries?

Hi Salesforce Gurus,

 

How to build Dynamic SOSL queries? Let’s say I have one requirement where, there are two fields, by which we search for a few ample of records.  

                Requirement-1: If end user search with only single field value keeping other field empty also, he would be able to get some records as a search results.

                Requirement-2: if end user may give the both the fields value and search again, that time also he would be able to get some records as a search results.

          How to build Dynamic SOSL query for this? Anybody can help me out in this scenario??

Best Answer chosen by Admin (Salesforce Developers) 
sourav046sourav046

Are you looking for some code like this ?Have a look I created it almost 4months ago.But i"m not sure this will help you or not .

public class SearchCon{
    public String conName{get;set;}
    public String conEmail{get;set;}
    public List<Contact> lst{get;set;}
    
    public List<Contact> onlyName()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Name=:conName];
        
        return lst;
         }
   
    public List<Contact> onlyEmail()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Email=:conEmail];
        
        return lst;
         }
   
    public List<Contact> nameplusemail()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Name=:conName and Email=:conEmail];
        
        return lst;
         }      
}

 

All Answers

sourav046sourav046

Are you looking for some code like this ?Have a look I created it almost 4months ago.But i"m not sure this will help you or not .

public class SearchCon{
    public String conName{get;set;}
    public String conEmail{get;set;}
    public List<Contact> lst{get;set;}
    
    public List<Contact> onlyName()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Name=:conName];
        
        return lst;
         }
   
    public List<Contact> onlyEmail()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Email=:conEmail];
        
        return lst;
         }
   
    public List<Contact> nameplusemail()
        {
        lst=[SELECT CreatedDate,Email,IsPersonAccount,Name FROM Contact where Name=:conName and Email=:conEmail];
        
        return lst;
         }      
}

 

This was selected as the best answer
Sujan Kumar ReddySujan Kumar Reddy

 

Thank you Sourav, got the logic from you code snippet..