Professional Documents
Culture Documents
SOQL & SOSL in Salesforce
SOQL & SOSL in Salesforce
SYNTAX:
SELECT <Field API NAME1>, <Field API NAME1>, <Field API NAME1> FROM <ObjectName> [Conditions]
Example:
Select Id, FirstName, LastName from contact.
select Id, Name, Rating,Industry From Account.
Where to Write SQOL Query
1. we can write SOQL query using a Query Editor in Developer Console.
2. we can write SOQL query using a Apex Programming in Developer Console.
Governor Limits for SOQL:
1. We can Only use 100 SOQL query in a single Transaction.
2. If we Tries to use more Than 100 then we will get an Exception.
3. "system.limitException Too Many SOQL : 101".
4. we can fetch only "50,000" Record at a Time using SOQL query.
Best Practices for SOQL:
1. Always avoid using SOQL query inside the For Loop.
Exception If You write SOQL inside the forloop
System.LimitException: Too many SOQL queries: 101
2. Always Limit clause & Mention the Fields in SOQL query.
Type Of SOQL Queries
• Static SOQL.
• Dynamic SOQL.
1.Static SOQL.
1. Static SOQL queries should be enclosed with in a Square Bracket. [SOQL].
2. Static SOQL will execute Automatically.
Syntax:
[SQOL QUERY];
[Select Id, Name, Rating, Industry from Account];
[select Id, Name, Email_ Id from Hiring_Manager__c];
Examples for SOQL Queries:
Example 1:
Write an SOQL query to fetch The Account Record from Account Object with LIMIT.
Code:
Example 2:
Query The Opportunity Record in One Method & pass the Data To Another Method & print
using Debug.
Class Code:
Conditions in SOQL Queries [Clauses in SOQL]
1.WHERE Clause (WHERE Keyword)
Where Keyword is use to Filter the Result in SOQL query.
Syntax
Select field1, field2, field3 from Object_Name WHERE [Condition]
1.This Keyword is used to fetch the records from the object Along with the deleted Records.
Salesforce has One hidden field Name as "IsDeleted"
If Value of IsDeleted = False then Record is Not deletd from Database.
If Value of IsDeleted = True Then record is Deleted from Database.
Class Code:
IN Operator / In Keyword
Governor Limits:
1. We can Use Only 20 SOSL queries in a Single Transaction.
2. Each SOSL Queries can Fetch Only '2,000' Records at a Time.
3. SOSL Query can search for a keyword with in Max 20 Object at a Time.
4. We cannot Perform any "DML Operation" on SOSL queries.
5. SOSL query will return the data in the form of List of List of Sobject.
List<List<Sobject>> = [SOSL QUERY];