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
Isaac GomolkaIsaac Gomolka 

How to search for User information

Hi. I was wondering how/ whats the best way to do this: I need like an inputfield box or something on my Visualforce page where someone can type in a User ( preferably the Name like "Matt Smith", but could also be username or nickname or something). And that then makes sure that there is a user by that name in the Salesforce sandbox so that I can see that searched for User's information. I need this because I need to make something that will take that User the person searched for, and create a new User cloning that looked up user's profile, permission sets, etc(more detailed, but not important). So i need a way to search for a user and then be able to pull information from that user to use later when making a new User. Im sorry if this is stupid or confusing, I'm still new to Salesforce and could really use some help. Please and thank you soooo much!
Alain CabonAlain Cabon

"my Visualforce page where someone can type in a User ( preferably the Name like "Matt Smith", but could also be username or nickname or something)."

That is not standard in Salesforce so you need a new custom component instead of the standard inputfield component (with a dynamic behavior using jQuery).

Visualforce Autocomplete For Lookup Fields​: inspired by Jitendra Zaa and his fantastic blog post below.
https://opfocus.com/visualforce-autocomplete-lookup-fields/

AutoComplete Component in Visualforce using JQueryUI
http://www.jitendrazaa.com/blog/salesforce/ajax-based-autocomplete-component-in-salesforce-using-jquery-ui-and-json/

Based on the technique of jQuery Autocomplete: Enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.  https://jqueryui.com/autocomplete/

That is not trivial at all and quite complicated to do.
Isaac GomolkaIsaac Gomolka
Hi Alain, thanks for your response! What i need doesnt necessarily have to be an autocomplete thing. ITt could just be you type in the name and click a button and it says wether he is a user or not, or something of the sort. I just feel like it would be easiest for whoever is using this to be able to type in a name rather than a username or nickname or something. And then i need to be able to see that users info. Do u know how I would do that? Like if someone types in Matt Smith and there is a Matt Smith user, then i need my code to be able to pull Matt Smith's information so I can copy it to a new User. Do you know how to do something like that? Thanks so much for the help!
Alain CabonAlain Cabon
Hi Isaac,

"you type in the name and click a button and it says wether he is a user or not, or something of the sort.":

That is a common technique when you need to control a remote name with a webservice in a referential base indeed but for user names in your Salesforce org (some hundreds people until some thousands max) you can have the autocomplete search effectively and that will blow your client away.
Autocomplete is also useless indeed if you just want to verify the existence of a complete name that you type in very precisely each time and the search of duplicate can be very complicated as soon as it is not the exact values that you compare.

For the duplicates : Matching Algorithms: Used with Matching Methods in Salesforce:
https://help.salesforce.com/articleView?id=matching_rules_matching_algorithms.htm&type=5&lang=en

The results can be a little disturbing for the first names in particular with SOSL (Bob and Robert will be find and return).

SOSL: WITH SPELL_CORRECTION is an optional clause that can be added to a SOSL query. When set to true, spell correction is enabled for searches that support spell correction. When set to false, spell correction is not enabled. The default value is true. The WITH SPELL_CORRECTION clause can be used in API version 40 or later. Example: The following SOSL statement disables spell correction on a search of accounts for the term San Francisco. FIND {San Francisco} IN ALL FIELDS RETURNING Account WITH SPELL_CORRECTION = false
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_soql_sosl.pdf

SOQL: easy and sufficient with just LIKE : The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.

Autocomplete + underlying SOSL WITH SPELL_CORRECTION : that will blow your client away.

Search Spelling Correction overview​:  Let's use the example of searching for 'John deo'. The results returned would  include 'John Doe' and various other matches
https://help.salesforce.com/articleView?id=000269438&language=en_US&type=1

in principle, that there is much further to go but you can just create a basic SOQL request like:

SELECT FirstName , LastName FROM user WHERE LastName like '%myString%'