I am creating a lookup on Account.Name field in Lightning. The problem is that Name field is not unique. So on searching for an account name, the lookup is displaying the same account name multiple times. The following is my code in Apex:

String searchQuery = 'FIND {' + sanitizedSearchString + '*} IN ALL FIELDS ';
searchQuery += 'RETURNING Account (Id, Name ';
searchQuery += 'WHERE RecordTypeId = \'somevalue\') ';
searchQuery += 'Limit 5';

I searched on google and found that there are ways to get distinct values in SOQL. Is it possible with SOSL?

  • But Account name can be same for different Account id. – NITHESH K Oct 6 '17 at 10:38
  • I know, but I don't need the Ids. I need unique account names. – snl Oct 6 '17 at 10:47
  • 2
    Get the result and add names into Set<string>, duplicate names will be eliminated. As set will not store duplicate element . – NITHESH K Oct 6 '17 at 10:49

Your question overall needs refinement. You will first of all need to define how to identify the winner account in case there are accounts with duplicate names.

You can collect the Accounts in a Map possibly

Map<Id,Account> mapAccounts = new Map<Id,Account>();
for(Account a:searchList)
// winning account logic here

use mapAccounts.keyset() to get a set of names of the accounts for you want to show for your lookup.

If you cannot determine a winner logic, an alternative would be to show another field along with the name of the account to help the user understand which account to choose. (It could be something like DUNS Number, Website, Account Code etc https://help.salesforce.com/articleView?id=account_fields.htm&type=5 )

Irrespective of how many Accounts you have in the Org, SOSL will only return a maximum of 2000 rows (Docs https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_limits.htm) I wouldn't worry about performance of handling 2000 rows.

From a end user perspective, 2000 rows is still a lot of data to choose from. You might want to look into reducing the results by prompting the user to add more/larger search terms.


You have do something like this get unique values. Get the result and Assign names into set of String. that will eliminate your duplicate value.

List<Account> searchList = search.query(searchquery)[0];

system.debug('searchList' + searchList);

set<string> accountNames=new set<string>();
for(Account a:searchList)

system.debug('accountNames' + accountNames);
  • Imagine this is the logic for my lookup which is based on Account object which contains million of records. This will give rise to performance issues. – snl Oct 6 '17 at 15:11
  • @sonal Yes off-course, but how will show millions of suggestion to user in one Go? You have to put some limit to your sosl for good performance . – NITHESH K Oct 9 '17 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.