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 Jun 13 '13 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 Jun 14 '13 at 3:35
  • @eyescream: Nothing happens as the data only exits in the unit test's scope ;) – Robert Sösemann Jun 14 '13 at 7:03
  • Please vote for this idea to help you better test your query: success.salesforce.com/ideaView?id=08730000000Lf1KAAS – Charles Koppelman Oct 7 '15 at 15:54
12

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. – Robert Sösemann Jun 14 '13 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?! – Robert Sösemann Jun 17 '13 at 7:35
  • 2
    I remember hitting that bug back in 2010, can't believe I forgot about it when I saw your question. – ca_peterson Jun 17 '13 at 9:11
  • Not to mention that my org already is on Summer '13... – Robert Sösemann Jun 17 '13 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 Jun 20 '13 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 Apr 9 '14 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 Jun 13 '13 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 Jun 13 '13 at 22:43

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.