SOSL stands(Salesforce object search language)
|
SOQL stands for(Salesforce object query language)
|
Works on multiple objects at the same time.
|
Need to write different SOQL for multiple objects.
|
Cannot be used in Triggers.
|
Can be used in Triggers.
|
Unlike SOQL, which can only query one object at a time and multiple objects only if they are related to each other, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.
Sample SOSL:
FIND {test}
IN Name FIELDS
RETURNING
Account( Id, Name ), Contact( Id, Name )
Sample Apex Code:
String searchQuery = ‘FIND {test} IN Name FIELDS RETURNING Account( Id, Name ), Contact( Id, Name )’;
List < List < sObject > > searchResult = search.query( searchQuery );
List < Account > listAccount = ( List < Account > )searchResult.get( 0 );
List < Contact > listContact = ( List < Contact > )searchResult.get( 1 );
for ( Account acc : listAccount ) {
System.debug( acc );
}
for ( Contact con : listContact ) {
System.debug( con );
}