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
B TulasiB Tulasi 

SOSL

Hi All,

I have to give one presentation for SOSL. So, plz provide information for that.
What is SOSL  and what is use of this and syntax and with example. difference between soql and sosl

Thanks in advance
Thulasi.
SRKSRK
You can easy search that on google 

Use the Salesforce Object Search Language (SOSL) to search your organization’s Salesforce data for specific information. WithSOSL, you can search all objects—including custom objects—that you have access to in a single query.

It is used for wide range seach,

U can understand like this when you search in Salesforce Global search it is kind of SOSL, it retun general search result 

and when we create list view on saparate object and condtion it kind of SOQL, it return spefic search results
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_about.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm


Comparing SOSL and SOQL

Like Salesforce Object Query Language (SOQL), SOSL allows you to search your organization’s Salesforce data for specific information. Unlike SOQL, which can only query one object at a time, a single SOSL query can search all objects—including custom objects—to which you have access. The API executes the search within the specified scope and returns to you only the information that is available to you based on the user permissions under which your application has logged in.

1) Use SOQL with the query() call to select records for a single object.
2) Use SOSL with the search() call to find records for one or more objects. The search() call searches most text fields on an object. See Search Scope for information on the fields searched.

Please let us know if this will help u

Thanks
Amit Chaudhary
B TulasiB Tulasi
Hi Amit,


Thank You for ur reply. Already i have in above links, I searched in google. But, I need program for that , one real time example for exlamation purpose.

Thanks
Thulasi
SRKSRK
HI Tulasi,

i have write the below code just to explane you the diffrence there may be some exception in saving and running (Hope you can fix small issues around)

SO there is a VF page have drop down with opttion as All, Account, Contact and Lead and if you select All and just enter data in serach box and click on serach box it serach in all 3 object using SOSL 
and if you select a spacific object it show you the fiel related to that object and once you select the field and enter the serach text in serach box and enter seach button 
then throw SOQL it search in spacific object on that specific field

VF page

<apex:page controller="objectController">
<apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection columns="2">

              <apex:pageBlockSectionItem >
                  <apex:outputlabel value="Object Names :"/> 
                      <apex:actionRegion >  
                            <apex:selectList value="{!selectedObject}" size="1">
                                    <apex:selectOption itemValue="All" itemLabel="All" />
                                    <apex:selectOption itemValue="Account" itemLabel="Account" />
                                    <apex:selectOption itemValue="Contact" itemLabel="Contact"/>
                                    <apex:selectOption itemValue="Lead" itemLabel="Lead" />
                                    <apex:actionSupport event="onchange" rerender="myFields"/>
                            </apex:selectList>
                     </apex:actionRegion>                         
              </apex:pageBlockSectionItem>

              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="Field Names (Please select only text type Field other case not handeld for SOQL in Class) :"/>   
                      <apex:outputPanel id="myFields">   
                        <apex:actionRegion >  
                           <apex:selectList value="{!selectedField}" size="1">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputlabel value="Seach for text" ></apex:outputlabel>
                <apex:inputtext value="{!searchstring}"/>
            </apex:pageBlockSectionItem>
          </apex:pageBlockSection>
        <apex:commandbutton value="Search" action="{!Search}">
        </apex:commandbutton>
      </apex:pageBlock>
  </apex:form>
</apex:page>


Apex Class

public class objectController
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    public string searchstring{get;set;}
    Public objectController()
    {   
        selectedObject = 'All';
        searchstring = null;
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }

    public List<SelectOption> getObjectFields() 
    {
        if(selectedObject != 'All)
        {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;
        }
    }  

    public void Search()
    {
        if(searchstring != null)
        {
            if(selectedObject != 'All)
            {
                String searchquery='FIND\'+'searchstring+'*\'IN ALL FIELDS RETURNING Account(id,name),Contact, Lead'; 
                List<List<SObject>>searchList=search.query(searchquery);
                system.debug('&!@$#&$!@#%&$!@%&#'+searchList);
            }
            else
            {
                String resolvedField1 = myVariable.field1__c;
                strign Dynquery = 'SELECT Id FROM '+ selectedObject+' WHERE '+selectedField +'= \'' + searchstring+ '\'';
                List<sObject> sobjList = Database.query(Dynquery);
                system.debug('&!@$#&$!@#%&$!@%&#'+sobjList);
            }
        }
    }
}