SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries.
For example, you could retrieve a list of accounts that are named Acme:
List<Account> aa = [select id, name from account where name ='Acme'];
From this list, you can access individual elements:
if (!aa.isEmpty()){// Execute commands}
You can also create new objects from SOQL queries on existing ones. The following example creates a new contact for the first account with the number of employees greater than 10:
Contact c =new Contact(account = [select name from account
where NumberofEmployees > 10 limit 1]);
c.FirstName ='James';
c.LastName ='Yoyce';
Note that the newly created object contains null values for its fields, which will need to be set.
The count method can be used to return the number of rows returned by a query. The following example returns the total number of contacts with the last name of Weissman:
Integer i = [select count() from contact where lastname ='Weissman'];
You can also operate on the results using standard arithmetic:
Integer j = 5 * [select count() from account];
SOSL Statements 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.
For example, you can return a list of accounts, contacts, opportunities, and leads that begin with the phrase map:
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead];Account [] accounts = ((List<Account>)searchList[0]);
SOQL(Salesforce Object Query Language) 1)Using SOQL we can Search only on one object at a time. 2)We can query on all fields of any datatype 3)We can use SOQL in Triggers and classes. 4)We can perform DML operation on query results.
SOSL(Salesforce object Search Language) 1)Using SOSL we can search on many objects at a time. 2)We can query only on fields whose data type is text,phone and Email. 3)We can use in calsses but not in Triggers. 4)We cannot perform DML operation on search result
SOQL and SOSL Queries
SOQL Statements
SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries.
SOSL Statements 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.
For example, you can return a list of accounts, contacts, opportunities, and leads that begin with the phrase map:
Use the Salesforce.com Object Search Language (SOSL) to construct text searches in the following environments:
Unlike SOQL, which can only query one object at a time, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.
You use SOSL with the search() call in the API and SOQL with the query() call.
For more information, see:
SOQL:
http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_soql.htm
SOSL:
http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_sosl.htm
1)Using SOQL we can Search only on one object at a time.
2)We can query on all fields of any datatype
3)We can use SOQL in Triggers and classes.
4)We can perform DML operation on query results.
SOSL(Salesforce object Search Language)
1)Using SOSL we can search on many objects at a time.
2)We can query only on fields whose data type is text,phone and Email.
3)We can use in calsses but not in Triggers.
4)We cannot perform DML operation on search result
SOSL we can use in Triggers.Please find the below example.
trigger SOSLTriggerDemo on Account (before insert, before update)
{
List<List<SObject>> mySearchList = [FIND 'map*' IN ALL FIELDS RETURNING Account(Id,Name),Contact,Opportunity,Lead];
List<Account> myAcc = mySearchList[0];
System.debug('SOSL result account Details'+ myAcc);
System.debug('SOSL search Result Account Name:' + myAcc[0].Name);
}
Thanks,
Gopi.