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
nanostuffsnanostuffs 

FIND SOSL query not giving results while running apex test execution

Hello,

 

I have such code:

List<Contact> retContacts = [FIND :txtName IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)][0];

 

This code works and gives me few results when I am running it through a visualforce page. I need to upload my app for which I require min 75% code coverage, so I wrote test class which has:

mainObj.txtName = 'man*';
mainObj.SearchRecord();

 

It shows me only 57% code coverage and when i see test results I find the coverage is not going inside loop I have to process found results, in other words no results are returned.

 

If I use such statement it works and give perfect code coverage but I dont want to use this statement because it just searches Name field:

queryname = '%' + txtName + '%';

retContacts = [SELECT Id, Name,Phone,Email FROM Contact WHERE Name like :queryname];


Can anybody please help?

 

Thank you,

Nishant

http://nanostuffs.com/

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

You have to use a special syntax when testing SOSL - see the explanation here:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm

 

public class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = '001x0000003G89h';
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'test' 
                                         IN ALL FIELDS RETURNING 
                                            Account(id, name WHERE name = 'test' LIMIT 1)];
    }
}

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

In the test method before executing the SOSL query you should be insert Contacts with that data that is part of your SOSL query. Then call your Apex Class Function.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

nanostuffsnanostuffs

Thanks but still didnt work, I tried creating contacts and accounts and leads but still its showing any results :(

hitzhitz

Hi,

 

Can you please share code (method) which is not cover ?

BritishBoyinDCBritishBoyinDC

You have to use a special syntax when testing SOSL - see the explanation here:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm

 

public class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = '001x0000003G89h';
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'test' 
                                         IN ALL FIELDS RETURNING 
                                            Account(id, name WHERE name = 'test' LIMIT 1)];
    }
}

 

This was selected as the best answer
nanostuffsnanostuffs

Wow I could never imagine this would be solution, thanks it worked and strange why is it so with SOSL. Anyway, thanks for your help :)

Mosib KhanMosib Khan
// funtion having sosl query
public void soslQuery()
{
List<List<SObject>> searchList= [ FIND 'test' IN NAME FIELDS RETURNING Account(name), Lead(name), Contact(name), User(name)];
Account[] accList= ((List<Account>)searchList[0]);
Lead[] leadList= ((List<Lead>)searchList[1]);
Contact[] conList= ((List<Contact>)searchList[2]);
User[] userList= ((List<User>)searchList[3]);
System.debug(accList);
System.debug(leadList);
System.debug(conList);
System.debug(userList);
}
//a test class for the above function
@isTest
static void soslQuery(){
List<Account> testAccountLst = TestDataFactory.createAccounts(1);
List<Opportunity> testOppLst =
TestDataFactory.createOpportunity(1,testAccountLst.get(0).Id);
List<Lead> testLeadLst = TestDataFactory.createLead(1);
insert testAccountLst;
insert testOppLst;
insert testLeadLst;
Id [] fixedSearchResults= new Id[3];
fixedSearchResults[0] = testAccountLst[0].Id;
fixedSearchResults[0] = testOppLst[0].Id;
fixedSearchResults[0] = testLeadLst[0].Id;
System.Test.setFixedSearchResults(fixedSearchResults);
soqlObj.soslQuery();
}
Himanshu Maheshwari 8Himanshu Maheshwari 8
The solution suggested by BritishBoyinDC is correct. It's just that the record id you want to search should be dynamically placed and not statically entered.