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
YSPYSP 

Substring search on lookup fields in Lightning Component

We have created a custom lightning component with lookup fields to our account object. Users are currently able to search in the field however the results while typing are always ones that start with the same letters that are entered. Is is possible to have the search return results which contain the letters being typed anywhere in the account name?

Example: Account Name = Apple 
If you type "App" in the lookup field then Apple will appear as a result.
If you type "ppl"  in the lookup field then there are no results found. How can we make it so "ppl" will also return Apple as a result?

Code for Query:
 
public class Lookup {

    /**
     * Returns JSON of list of ResultWrapper to Lex Components
     * @objectName - Name of SObject
     * @fld_API_Text - API name of field to display to user while searching
     * @fld_API_Val - API name of field to be returned by Lookup COmponent
     * @lim   - Total number of record to be returned
     * @fld_API_Search - API name of field to be searched
     * @searchText - text to be searched
     * */
    @AuraEnabled 
    public static String searchDB(String objectName, String fld_API_Text, String fld_API_Val, 
                                  Integer lim,String fld_API_Search,String searchText ){
        
        searchText='\'%' + String.escapeSingleQuotes(searchText.trim()) + '%\'';

        
        String query = 'SELECT '+fld_API_Text+' ,'+fld_API_Val+
            			' FROM '+objectName+
            				' WHERE '+fld_API_Search+' LIKE '+searchText+ 
            			' LIMIT '+lim;
sachinarorasfsachinarorasf
Hi,

Please try this.
 
String query = '';
if(String.isNotBlank(searchText) && String.isNotEmpty(searchText)){
  searchText =searchText.trim();
}
if(String.isNotBlank(searchText) && String.isNotEmpty(searchText)){
  searchText = '\'%' +searchText +'%\'';
  query = 'SELECT '+fld_API_Text+' ,'+fld_API_Val+
	  ' FROM '+objectName+
	  ' WHERE '+fld_API_Search+' LIKE '+searchText+ 
	  ' LIMIT '+lim; 
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora