Thursday 20 August 2015

How to write a test class for the SOSL Query in Salesforce

             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.

Apex Class:

  1. public class SOSLController
  2. {
  3.     public static void findMatchingData(String str)
  4.     {
  5.         List<List<Sobject>> searchList = [FIND :str RETURNING Account(Id, Name), Lead];       
  6.         List<Account> accounts = ((List<Account>)searchList[0]);
  7.         if(accounts.size() > 0)
  8.         {
  9.             for(Account currAcc : accounts)
  10.             {
  11.                 // do your logic.
  12.             }
  13.         }
  14.     }
  15. }

Test Class:

  1. @isTest
  2. private class TestSOSLController
  3. {
  4.     static testMethod void insertAccount()
  5.     {
  6.         Account acc = new Account();
  7.         acc.Name = 'Test Account';
  8.         insert acc;
  9.        
  10.         Id [] fixedSearchResults= new Id[1];
  11.         fixedSearchResults[0] = acc.Id;
  12.         Test.setFixedSearchResults(fixedSearchResults);
  13.         SOSLController.findMatchingData('Test');
  14.     }
  15. }


Step 1: Create a record that you want to return in a search result.
Step 2: Create a list and assign the inserted id. 
Step 3: Use standard method Test.setFixedSearchResults and pass the above ID list.
Step 4: Call your apex method to test your functionality. 


7 comments:

  1. Introduction of cloud servers have created a greatest impact over data interpretation. Also they made a history in cloud computing. Handling data made much easier and more comfortable like never before. Your content tells the same about the emerging technology. Thanks for sharing this information in here.

    Salesforce training in chennai | Salesforce training institutes in chennai

    ReplyDelete
  2. Cloud servers are the best in safe guarding one's information thorugh online. Without this dedicated methodology many companies would have not existed at all. The same though has been furnished above. Thanks for sharing this worth while content in here. Keep writing article like this.

    Salesforce certification Training in Chennai | Salesforce developer training in chennai

    ReplyDelete
  3. Why don't you try creating a record that doesn't contain your search term 'Test'. You will find that it is still returned in the search results, which isn't very useful...

    ReplyDelete
  4. Great!it is really nice blog information.after a long time i have grow through such kind of ideas.
    thanks for share your thoughts with us.
    german learning classes in bangalore
    german coaching classes in bangalore
    German Training in Nolambur
    German Training in Saidapet

    ReplyDelete

  5. Hi,
    I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
    Selenium testing training
    Selenium Courses in Chennai
    Selenium training Chennai
    Software Testing Training in Chennai
    Software Testing Courses in Chennai
    Software Training Institutes in Chennai

    ReplyDelete
  6. This blog is more effective and it is very much useful for me.
    we need more information please keep update more.
    vmware training in bangalore
    vmware courses in bangalore
    vmware Training in Ambattur
    vmware Training in Guindy

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...