• Satish Kanchustambham 2
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
public class DynamicQuery {
public static void GetContactsBySearch(string searchtext)
  {
     if(searchtext != null && searchtext != '')
     {
         string contactquery = 'select id, Firstname, Lastname, Email from contact';
        if(pattern.matches('^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$' , searchtext))
           {
               system.debug('If user input is email');
               contactquery += 'Where Email =: searchtext';
           }
         else
         {
             system.debug('If user input is text value');
             searchtext += '%';
             contactquery += 'Where Lastname like : searchtext';
         }
          List<contact> lstcon =  Database.query(contactquery);  
           if(!lstcon.isempty())
           {
               system.debug('Collection size is..:'+ lstcon.size());
            for(contact con : lstcon)
            {
               system.debug('Contact records are ..:'+ con);
           }               
           }
     }
  }
}