Sunday, July 8, 2012

Salesforce Object Search Language (SOSL) in Salesforce

Introduction:--
SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type.The result lists are always returned in the same order as they were specified in the SOSL query. SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger. If a SOSL query does not return any records for a specified sObject type, the search results include an empty list for that sObject.

Example for SOSL

public class UseOfSOSL {
   
    //Lists
     List<Contact> contacts;
     List<Lead> leads;
     List<Account> accounts;
   
    //Constructor
     public UseOfSOSL() {
    
     //SOSL
     List<List<sObject>> results = [FIND '9999999998' IN Phone FIELDS
                                    RETURNING Contact(FirstName, LastName, Email, Phone),
                                    Lead(Id, Phone, FirstName, LastName), Account(Id, Phone, Name)];
     System.debug('##### Value of results'+results);
   
    }
}

Page

<apex:page controller="UseOfSOSL">
    <apex:form>
    </apex:form>
</apex:page>

Debug Log Detail for SOSL



Test Class

/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest
private class TestUseOfSOSL {
/*
 *  Purpose     :   Test class for UseOfSOSL
 * 
 *  Coverage    :   100%
 **/
   
    //Testing when there is absence of Contact with Specific Phone Detail
     static testmethod void myUnitTest() {
   
        //Insert Contact
        Contact contact = new Contact(lastName = 'Shar', Email = 'Test@Shar.com', FirstName = 'Test',
                                      Phone = '9999999999');
        insert contact;
       
        //Start Test from here
        Test.startTest();
       
        //Instantiate controller
        UseOfSOSL controller = new UseOfSOSL();
       
        //Asserting Values for Constructor
         List<Contact> listContact = [SELECT Phone From Contact WHERE Phone = '9999999998'];
         System.assertEquals(0,listContact.size());
      
        //Stop Test Here
        Test.stopTest();
    }
   
     //Testing when there is presence of Contact with Specific Phone Detail
     static testmethod void myUnitTest1() {
   
        //Insert Contact
        Contact contact = new Contact(lastName = 'Contact', Email = 'Test@contact.com', FirstName =    'Test', Phone = '9999999998');
        insert contact;
       
        //Start Test from here
        Test.startTest();
       
        //Instantiate controller
         UseOfSOSL controller = new UseOfSOSL();
       
       //Asserting Values for Constructor
        List<Contact> listContact = [SELECT Phone From Contact WHERE Phone = '9999999998'];
        System.assertEquals(1,listContact.size());
      
        //Stop Test Here
        Test.stopTest();
    }
}

Test Coverage Details

 






3 comments:

  1. hi.. how to display output of sosl on vfpage??

    ReplyDelete
  2. How do you actually test that your SOSL query is correct?
    In your test methods, you do run the SOSL query code, but you never assert that the results are those you expect.

    ReplyDelete