4

When you do a SOSL query - the result is in the form of multiple lists (one for each object you search).

List<List<sObject>> resultsList = 
    [FIND 'test' IN NAME FIELDS RETURNING Object__c(Id,Name) limit 10];

The line above does the SOSL query and now we have results in the "resultsList" list.

My problem is now getting those results into a simple "List<Object__c>" list.

When I try to assign the results like this, the list is always empty/null:

simpleList = ((List<Object__c>)resultsList[0]);

I've also tried assigning in array format, but always blank still, also tried using a for loop to iterate through the "resultsList" and add to my simpleList, but no dice.

So even though the SOSL query is definitely returning results, the list is always empty.

Anyone have any ideas? Thank you!

  • Have you verified that the results are actual rows of data? i.e. that resultsList isn't just containing a list of empty lists. – Nick Cook Jul 10 '14 at 1:21
  • Good question, but I'm pretty sure the result list has rows of data because when I run a debug log - I see something like this, 23:34:03.039 (39835190)|SOSL_EXECUTE_BEGIN|[72]|FIND :tmpVar1 IN NAME FIELDS RETURNING Object__c(Id,Name) LIMIT 10 23:34:03.085 (85719217)|SOSL_EXECUTE_END|[72]|Rows:10 So I know that the SOSL query is returning multiple results... @NickCook – Chris Federspiel Jul 10 '14 at 13:16
  • It would help to see the exact SoSL You were running. – Sebastian Kessel Dec 21 '15 at 7:43
1

You need to cast a lists to strict types

List<List<sObject>> resultsList = [FIND 'test' IN ALL FIELDS RETURNING Account(Id,Name), Case(Id, Description), Contact(Id, Title)];
System.debug( (List<Account>) resultsList[0]);
System.debug( (List<Case>) resultsList[1]);
System.debug( (List<Contact>) resultsList[2]);

The result: enter image description here

  • You don't have to. system.debug(resultsList[0]); is perfectly valid and will output results. So this cannot be the reason for the OP's issue – Eric Sep 4 '17 at 15:04
0

First verify that the required data is present inorder to get the result .

Then execute the SOSL in query editor of the developer console see that does your query returns some data . Note : while using sosl in query editor use curly braces {test} instead of 'test'.

Try also the list size in your apex using the size() metod and print it in debug log so u will come to know that the SOSL returns some data. eg: System.debug('--------------------- resultsList size'+resultsList .size());

0

This has to be something wrong with your data/expectations.

Please post a debug output of resultsList right after the query.

Also, there may be something else going on in your code, an errant getter/setter, code that clears or reassigns results, a misplaced comparison (= vs ==) etc. We will need to see more to really nail it down

To illustrate that what you are doing works in isolation (using Account as an example) the below code is doing exactly what you are and outputs correctly when results are returned.

List<sObject[]> results = [Find 'test' IN NAME FIELDS Returning Account(Name)];
system.debug((Account[])results[0]);

11:07:53:015 USER_DEBUG [2]|DEBUG|(Account:{Name=test acc, Id=0011400001ibwhXAAQ}, Account:{Name=Mold Test Experts, Id=001a000001PEpntAAD})

-1

If you launch SOSL in tests, it returns nothing, specifically list of empty lists, to assume that tests always will behave same way. There is workaround with

Test.setFixedSearchResults(List<ID>);

Example code:

ID[] fixedSearchResults = new ID[n]; 
//here you create array with ids of objects, which can be returned by SOSL.
//In this example n = 1
fixedSearchResults[0] = '001x0000003G89h'; //Example id;
Test.setFixedSearchResults(fixedSearchResults);

More info is on this site

  • Its not mentioned that he/shes doing it in test class? – Pranay Jaiswal Jun 11 '18 at 11:33
  • It isn't, so he/she could try it in tests. – Kacper Jun 11 '18 at 11:40
  • Welcome to SF.SE Kascper. I don't know if you realize it, but you've posted an answer to a question that was asked 4 yrs ago by a user who doesn't appear to have remained active. As noted by others, nothing in his question (or tags) indicated he was asking the question in a test class context. I recommend you visit the help center and read the section on "Answering". – crmprogdev Jun 11 '18 at 12:05

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.