SOQL Queries --> Salesforce Object Query Language.
- By using SOQL Queries, we can fetch One / More / All records from the required object from the Database.com
- By using SOQL Queries, We can fetch the records from one or more objects.
Query Definition:
- It is a request to Database.com to get the required records from the objects.
- Upon fetching the records, we can add one or more user-defined conditions to filter the records.
- Note: SOQL Queries should be prepared with a "SELECT" statement.
Syntax: [Select Id, Name From ObjectName];
Governor Limits:
- We can have max. of 100 SOQL Queries in a transaction.(If the user tries to execute more than 100 SOQL Queries in a transaction, salesforce will raise an exception "System. Limit Exception. Too Many SOQL Queries: 101)
- Each SOQL Query can return a max. of 50,000 records.
Best Practices:
- Upon using SOQL Queries, avoid the usage of SOQL Queries inside the FOR loop.
- While preparing the Query, specify the required column names in the list. Don't fetch the unnecessary fields data from the object.
We can invoke the SOQL Queries by using Apex Programming. Apex categorizes the SOQL Queries as below.
- Static SOQL Queries
- Dynamic SOQL Queries
Example of SOQL Query.
- Write a SOQL Query, to fetch Account Id, Name, Rating, Industry values.
select id, name, rating, industry from Account
- Write an SOQL to fetch Lead Record Details.
select id, firstname, lastname, company, status, email, phone, fax from Lead
- Write a SOQL Query, to fetch all the user details..
select id, firstname, lastname, username, alias, email, profile.id, isactive from user
If the SOQL Query returns Only one record, then we need to store the result by defining a variable of the associated type.
Ex:
Account acc = [select id, name, rating, industry from Account];
if(acc != null)
{
system.debug('Account id is....: '+ acc.id);
system.debug('Account Name is...: '+ acc.name);
system.debug('Rating Value is...: '+ acc.rating);
system.debug('Industry Value is...: '+ acc.industry);
}
SOSL Queries:
=============
By using SOQL Queries, we can search for the specific content in Only One Object at a time, by adding the required conditions with the help of the "WHERE" clause.
To search for the keyword, in multiple objects, we need to write multiple SOQL Queries. Which causes the "Too Many SOQL Queries: 101" Exception.
To avoid this problem, salesforce provides a Search Engine called "SOSL Queries".
SOSL --> Salesforce Object Search Language.
By using SOSL Queries, we can search for specific content in multiple objects in the salesforce Database.
It will search for the content in all fields inside the object.
If any of the Object records, matching the specified search text criteria, then it will return the records to the user.
(Note: Where clause can't be applicable in "SOSL Queries")
Note:
SOSL Queries should be prepared with the help of the "FIND" keyword.
Syntax:
FIND 'searchtext*' IN ALL FIELDS RETURNING
<ObjectName1>(<Colum1>, <Colum2>,<Colum3> ,.....<ColumN>),
<ObjectName2>(<Colum1>, <Colum2>,<Colum3> ,.....<ColumN>),
...
...
<ObjectName20>(<Colum1>, <Colum2>,<Colum3> ,.....<ColumN>)
Ex:
FIND 'test*' IN ALL FIELDS RETURNING
Account(id, name, rating, industry, active__c),
Lead(id, firstname, lastname, title, city, state),
Position__C(id, name, Location__c, Position_status__C),
Contact(Id, firstname, lastname, email, phone, fax),
Candidate__C (id, name, location__c, notice_period__C)
Governor Limits:
----------------
1. A transaction can have max. of 20 SOSL Queries.
2. Each SOSL Query can return max. of 2,000 records as the result.
3. Each SOSL Query can search for the records in max. of 20 Objects.
Note:
We can't perform the "DML Operations" on the SOSL Query results.
Salesforce provides 2 types of SOSL Queries.
1. Static SOSL Queries:
All the Static SOSL Queries should be enclosed inside the square braces "[ ]".
We can't add the "Search Text" in the SOSL Query dynamically at runtime. It should be hardcoded upon writing the query.
Ex: [ SOSL Query ];
Static SOSL Queries will be executed automatically by default.
SOSL Queries will return the results in the form of "List<List<SObject>>".
Syntax:
List<List<SObject>> results = [ SOSL Queries ];
2. Dynamic SOSL Queries.
By using this approach, we can include the search text dynamically at runtime inside the SOSL Query.
Each Dynamic SOSL Query should be getting prepared and stored inside a string variable.
Ex:
string searchQuery = ' SOSL Query ';
We need to execute the SOSL Query manually with the help of the "Search. Query()" method.
Dynamic SOSL Query will return the result in the form of "List<List<SObject>>".
Ex:
List<List<SObject>> lstResults = Search.Query(searchQuery);
Note:
Upon searching for the keyword in multiple objects, we can specify the Search Group. Which indicates, in what type of fields the keyword has to be get searched.
Options:
IN ALL FIELDS:
It will search in All the Fields in the Object.
IN NAME FIELDS:
It will search for the Keyword only in "Name Fields".
IN EMAIL FIELDS:
It will search for the Keyword only in "Email Fields".
IN PHONE FIELDS:
It will search for the Keyword, only in "Phone fields".
Ways to invoke the SOSL Queries:
--------------------------------
We can invoke the SOSL Queries with the below options.
1. By using "Query Editor" from Developer Console.
2. By using Apex Programming.
/*
Prepare a SOSL Query, to search for the "test" keyword in Account and Lead Objects, by using Query Editor.
*/
find {test} IN ALL FIELDS RETURNING
Account(id, name, rating, industry, type),
Lead(id, firstname, lastname, email,company, status)
/*
Write a SOSL Query, to search for the keyword "apex" in Account, Lead, User, and Position Object.
*/
find {apex} IN ALL FIELDS RETURNING
Account(id, name, rating, industry, type),
Lead(id, firstname, lastname, email,company, status),
User (id, firstname, lastname, username, email, title),
Position__C(id, name, Location__C, position_status__C, close_date__c)
إرسال تعليق