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
Gauri Gaikwad 13Gauri Gaikwad 13 

How to count number of records in SOSL query

SOSL query: i want to count number of records of .com in account, leads, opportynity object

FIND {.com}, count()
IN ALL FIELDS
RETURNING Opportunity(AccountId), Lead(company),Account(Name)
 
Best Answer chosen by Gauri Gaikwad 13
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Gauri,

I dont think we can use Count() in sosl query. Instead you have to count the size of each object and add them to find the total returned records in apex as shown below.
 
List<List<sObject>> searchList = [FIND '.com' IN ALL FIELDS RETURNING Opportunity(AccountId), Lead(company),Account(Name)];
integer count= searchList[0].size() +searchList[1].size() +searchList[2].size();
system.debug('count'+count);

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Gauri,

I dont think we can use Count() in sosl query. Instead you have to count the size of each object and add them to find the total returned records in apex as shown below.
 
List<List<sObject>> searchList = [FIND '.com' IN ALL FIELDS RETURNING Opportunity(AccountId), Lead(company),Account(Name)];
integer count= searchList[0].size() +searchList[1].size() +searchList[2].size();
system.debug('count'+count);

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Gauri Gaikwad 13Gauri Gaikwad 13
Thanks