Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

KhumedSFDC & CloudHAK InfoTech

Salesforce SOQL & SOSL

SOQL & SOSL In Salesforce Apex

SOQL: Salesforce Object Query Language


1. By Using SOQL we can Fetch the Records from the Object in Salesforce.
2. We can fetch one or Multiple records from Object using Salesforce.
3. We can fetch the data or records from Database (Object) on the basis of
4. condition.
5. SQOL query will always start with the keyword "select / SELECT".

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]

Logical Operators with WHERE Keywords In SOQL:


• AND.
• OR.
• NOT.
Passing Value Dynamically to SOQL Query at Runtime:
Example 1:
Fetch The Vehicle Data on the basis of Car maker from Vehicle Object. Passing The value to SOQL query
Dynamically at Runtime.

ALL Rows Clause / Keyword:

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

1. Custom Labels in Apex.


2. Custom Meta Data in Apex.
Real Time Example:
1. Fetch Contact Records with AccountId! = null.
2. Store That AccountId in a Set collection.
3. Fetch Account record by using IN operator using Filter as a Set.
Dynamic SOQL Queries:
• By using Dynamic SOQL query we can prepare query at Run time with the required
conditions.
• We need to write dynamic SOQL query in String Format.
Syntax:
String Myquery = 'Dynamic SOQL Query';
Example:
String myQuery = 'Select Id,Name from Account';
• We need to Execute Dynamic SOQL query by using a Standard Method given by Apex
Programming called as “Database.Query()".
• Dynamic SOQL query will always return Data in List Collection Format.
Syntax For Executing Dynamic SOQL:
string myquery = 'Select Id,Name,Rating From Account';
List<Account> myLst = Database.Query(myquery);
SOSL: Salesforce Object Search Language:
1. SOSL: Salesforce Object Search Language.
2. SOSL is used to search for a specific Keyword from Multiple Object at a time.
3. SOSL is always start from a keyword called "FIND"

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];

You might also like