18

I have a Custom Object that contains a long text area with 32.000 chars full of comma-separated Ids. I need to find all such records that contain one or more ids.

I tried to implement this in this test:

@isTest
private static void soslWorksAsExpected() {
    // Setup
    List<Id> ids = insertOppsWithAmountZeroAndReturnIds(10);
    SObjectWork__c pendingWork = new SObjectWork__c( 
                                    ltxt_SObjectIds__c = '' + ids.get(2) + ',' + ids.get(7),
                                    txt_ProcessorClassName__c = 'SObjectWorkTestHelper.ExampleSObjectProcessor',
                                    pkl_CurrentState__c = 'QUEUED');
    insert pendingWork;

    List<Id> pendingWorkIds = new List<Id>{pendingWork.Id};
    System.debug('# pendingWorkIds' + pendingWorkIds);
    Test.setFixedSearchResults(pendingWorkIds);

    // Execute
    String orPhrase = String.join(pendingWorkIds, ' OR ');
    System.debug('# orPhrase' + orPhrase);
    List<SObject> soqlResults = [SELECT Id, ltxt_SObjectIds__c FROM SObjectWork__c WHERE pkl_CurrentState__c = 'QUEUED'];
    System.debug('# soqlResults' + soqlResults);
    List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ALL FIELDS RETURNING SObjectWork__c(Id, ltxt_SObjectIds__c)];
    System.debug('# soslResults' + soslResults);

    // Verify
    System.assert( !soslResults.isEmpty() );    
    System.assert( !soslResults.get(0).isEmpty() );     
}

but it always fails. As I can see from the debug statements everything works. Except the

Test.setFixedSearchResults(pendingWorkIds);

does not change anything.

What am I doing wrong?

I checked if I can search for this record type in the Search and it worked.

4
  • What happens if you'll type your phrase in the Sidebar search (or the top of the page search box if you have Chatter enabled)?
    – eyescream
    Commented Jun 13, 2013 at 21:42
  • 1
    Your syntax actually seems fine, and I've confirmed that running the same type of query (including an OR query as a parameterized clause) works for me. Can you post some more detail on exactly the query you are running, and exactly the text value it seems to be not finding?
    – jkraybill
    Commented Jun 14, 2013 at 3:35
  • @eyescream: Nothing happens as the data only exits in the unit test's scope ;) Commented Jun 14, 2013 at 7:03
  • Please vote for this idea to help you better test your query: success.salesforce.com/ideaView?id=08730000000Lf1KAAS Commented Oct 7, 2015 at 15:54

3 Answers 3

13

You didn't mention it was an unit test context :)

Testing SOSL is somewhat messy because of indexing delay. You might have to do manual tests of your functionality first and get back to the unit test once you're happy with it. Then in actual code play with Test.setFixedSearchResults().

Check http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm for more info and example?

1
  • 1
    Sorry @eyescream. I had to reopen the question as this just is not working as proposed or documented. Commented Jun 14, 2013 at 11:35
10

The reason why my code was not working - and this really makes no sense to me - is a missing package namespace:

I replaced this line:

List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ALL FIELDS RETURNING SObjectWork__c(Id, ltxt_SObjectIds__c)];

with this one

List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ALL FIELDS RETURNING NAMESPACE__SObjectWork__c(Id, ltxt_SObjectIds__c)];
6
  • Is there any way to stop an open bounty?! Commented Jun 17, 2013 at 7:35
  • 2
    I remember hitting that bug back in 2010, can't believe I forgot about it when I saw your question. Commented Jun 17, 2013 at 9:11
  • Not to mention that my org already is on Summer '13... Commented Jun 17, 2013 at 9:31
  • @Robert nope, no way to cancel a bounty. Check out salesforce.stackexchange.com/help/bounty : "All bounties are paid for up front and non-refundable under any circumstances."
    – Benj
    Commented Jun 20, 2013 at 2:30
  • Did you find a way to do this in a non package specific way such that it will work if you are in the packaging org or in a development org?
    – jwolf
    Commented Apr 9, 2014 at 15:31
0

I haven't done much SOSL, but to me it looks like it will look for the phrase 'wordA OR wordB OR wordC' in all fields, including the OR's. Should it be something like this?

String wordA = 'wordA';
String wordB = 'wordB';
String wordA = 'wordC';
List<List<SObject>> soslResults = [FIND :wordA OR :wordB OR :wordC IN ALL FIELDS RETURNING MyObject__c(ltxt_Text__c)];
2
  • Not sure that's the case... If you have a Developer Edition - go to workbench.developerforce.com and compare results of FIND {arizona OR burlington} vs. FIND {"arizona OR burlington"} (or adapt to your dataset if you don't have DE). Also salesforce.com/us/developer/docs/soql_sosl/Content/… seems to be putting the AND/OR within the search container {}
    – eyescream
    Commented Jun 13, 2013 at 22:22
  • 1
    Yeah, that's correct. His syntax looks fine, maybe there's no records that have any of those words in them, or he's running as a user that doesn't have access to those records or the fields that contain the text.
    – Sean
    Commented Jun 13, 2013 at 22:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .