You need to sign in to do that
Don't have an account?
chaithaly gowda
SOSL query not returning the value in the test class
Hi
I am trying to get the result in testclass from SOSL query ,but the SOSL is not returning any values even when there is a matching test data.
Please refer below code:
User user=new User(FirstName='Test',
LastName='Contact',
Alias='tcont',
email='test.contact@xyz.com',
Username='test.contact@xyz.com',
CommunityNickname='cont',
profileId=sysAd.id,
EmailEncodingKey=DummyUser.EmailEncodingKey,
TimeZoneSidKey=DummyUser.TimeZoneSidKey,
LocaleSidKey=DummyUser.LocaleSidKey,
LanguageLocaleKey=DummyUser.LanguageLocaleKey
);
insert user;
System.RunAs(user){
Account acc=new Account(Name='Test',
RecordTypeId=accRecMap.get('Con').getRecordTypeId()
);
insert acc;
Contact cont=new Contact(Account=acc,
FirstName='Test',
LastName='Contact',
AccountId = acc.Id,
RecordTypeId=contRecMap.get(' Contact').getRecordTypeId()
);
insert cont;
String userName=Userinfo.getName();
Map<String, Schema.RecordTypeInfo> contactRecMap= Schema.SObjectType.Contact.getRecordTypeInfosByName();
String searchQueryCon = 'FIND \'' + userName + '\' IN ALL Fields RETURNING Contact(Id, Name,FirstName, LastName, Title, Email, Phone, Description)';
System.debug(searchQueryCon);
System.debug(cont);
List<List<Contact>> contactListNew = search.query(searchQueryCon);
System.debug(contactListNew);
}
I am trying to get the result in testclass from SOSL query ,but the SOSL is not returning any values even when there is a matching test data.
Please refer below code:
User user=new User(FirstName='Test',
LastName='Contact',
Alias='tcont',
email='test.contact@xyz.com',
Username='test.contact@xyz.com',
CommunityNickname='cont',
profileId=sysAd.id,
EmailEncodingKey=DummyUser.EmailEncodingKey,
TimeZoneSidKey=DummyUser.TimeZoneSidKey,
LocaleSidKey=DummyUser.LocaleSidKey,
LanguageLocaleKey=DummyUser.LanguageLocaleKey
);
insert user;
System.RunAs(user){
Account acc=new Account(Name='Test',
RecordTypeId=accRecMap.get('Con').getRecordTypeId()
);
insert acc;
Contact cont=new Contact(Account=acc,
FirstName='Test',
LastName='Contact',
AccountId = acc.Id,
RecordTypeId=contRecMap.get(' Contact').getRecordTypeId()
);
insert cont;
String userName=Userinfo.getName();
Map<String, Schema.RecordTypeInfo> contactRecMap= Schema.SObjectType.Contact.getRecordTypeInfosByName();
String searchQueryCon = 'FIND \'' + userName + '\' IN ALL Fields RETURNING Contact(Id, Name,FirstName, LastName, Title, Email, Phone, Description)';
System.debug(searchQueryCon);
System.debug(cont);
List<List<Contact>> contactListNew = search.query(searchQueryCon);
System.debug(contactListNew);
}
Have you tried using a hard coded name in the SOSL instead of userName and is the string String userName=Userinfo.getName(); populated?
I recommend testing this by writing a query that is no dynamic. Try a simple query like this
searchvalue = '/*est/*';
[FIND :searchvalue IN ALL FIELDS RETURNING Account (Id, Name)];
Let me know if it returns results
you have to fake your response in testclass for sosl: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_SOSL.htm
Best regards,
Christian
The solution is found on this article :
Adding SOSL Queries to Unit Tests :
To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of record IDs that are returned by the search. All SOSL queries that take place later in the test method return the list of record IDs that were specified by the Test.setFixedSearchResults method.
Here is a simple example :
@isTest
private 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)];
}
}
Best regards,
Enrique
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_SOSL.htm