+ Start a Discussion
The new LearnerThe new Learner 

Basic search functionality for users

Hi Experts,

I need Basic Search functionality where i need to search for Users information with the help of Email Id or User Number or Bid Number only one search box has to be there and also if there are related information i need to throw an error.

Please help me out.

Thanks in advance
Best Answer chosen by The new Learner
Etienne DKEtienne DK

searchText = searchText + '%';


results = [SELECT Id, Name, Email, User_Number__c  FROM User WHERE 
  (Name LIKE :searchText) OR 
  (Email LIKE :searchText) OR
  (User_Number__c LIKE :searchText)];

system.debug('Found = ' + results.size());
// this does fuzzy matching, for exact matches ditch the % and use equals . (Name = :searchText)
// this assumes all search fields are strings

All Answers

Etienne DKEtienne DK
I'm not sure I understand the question but assuming you need to search across certain fields on User objects then native functionality will do, no coding required.
Look at the the Object Manager > User  > Search Layouts
And here too.
https://help.salesforce.com/articleView?id=search_fields_lex.htm&type=5

Else you're into SOQL territory I would imagine....
The new LearnerThe new Learner
Hi Etlenne,

I need to write search functinality , i wrote but its not woking below is my code.


public class UserSearch {
 
  String searchText;
   List<user> results;
    
public String getSearchText() {
      return searchText;
   }
 
   public void setSearchText(String s) {
      searchText = s;
   }
 
   public List<user> getResults() {
      return results;
   }
    public PageReference search() {
        
            results = [select Name,User_number__C,Email,User_ID__c from user where Name=:searchText ];
        
       
         
       return null;
    }
 
}
Etienne DKEtienne DK

searchText = searchText + '%';


results = [SELECT Id, Name, Email, User_Number__c  FROM User WHERE 
  (Name LIKE :searchText) OR 
  (Email LIKE :searchText) OR
  (User_Number__c LIKE :searchText)];

system.debug('Found = ' + results.size());
// this does fuzzy matching, for exact matches ditch the % and use equals . (Name = :searchText)
// this assumes all search fields are strings
This was selected as the best answer
The new LearnerThe new Learner
Hi ,

Thanks for the udpate its working fine, i need one more amendent if i wont found any data related name or user number or email , i need to show an error email. how can i add , kindly help me out.