Skip to content

List of Frequently Used SOQL Queries

List of Frequently used SOQL Queries.

1. What is SOQL?

The Salesforce Object Query Language (SOQL) is used to search your Salesforce data for specific information. SOQL is similar to the Structured Query Language (SQL) but is designed specifically for Salesforce data.

SQL Select Statement

SELECT one or more fields 
FROM an object 
WHERE filter statements and, optionally, results are ordered 

Example:
SELECT Id, Name
FROM Account
WHERE Name = 'DYDC'

2. When to use SOQL?

You can use SOQL when you want to:

  • Retrieve data from a single object or from multiple objects that are related to one another
  • Count the number of records that meet specified criteria
  • Sort results as part of the query
  • Retrieve data from number, date, or checkbox fields

3. SQL Select Syntax

SOQL query syntax consists of a required SELECT statement followed by one or more optional clauses, such as TYPEOFWHEREWITHGROUP BY, and ORDER BY.

SELECT fieldList [subquery][...]
[TYPEOF typeOfField whenExpression[...] elseExpression END][...] FROM objectType[,...] 
    [USING SCOPE filterScope]
[WHERE conditionExpression]
[WITH [DATA CATEGORY] filteringExpression]
[GROUP BY {fieldGroupByList|ROLLUP (fieldSubtotalGroupByList)|CUBE (fieldSubtotalGroupByList)} 
    [HAVING havingConditionExpression] ] 
[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]
[LIMIT numberOfRowsToReturn]
[OFFSET numberOfRowsToSkip]
[FOR {VIEW  | REFERENCE}[,...] ]
      [ UPDATE {TRACKING|VIEWSTAT}[,...] ]
SyntaxDescription
fieldList subquerySpecifies a list of one or more fields, separated by commas, that you want to retrieve from the specified object.
typeOfFieldA polymorphic relationship field in objectType or a polymorphic field in a parent of objectType that can reference multiple object types.
whenExpressionA clause of the form WHEN whenObjectType THEN whenFieldList. You can have one or more whenExpression clauses inside a TYPEOF expression.
elseExpressionA clause of the form ELSE elseFieldList. This clause is optional inside a TYPEOF expression.
objectTypeSpecifies the type of object that you want to query()
filterScopeSpecifies the filterScope for limiting the results of the query.
conditionExpressionIf WHERE is specified, determines which rows and values in the specified object (objectType) to filter against. If unspecified, the query() retrieves all the rows in the object that are visible to the user.
filteringExpressionIf WITH DATA CATEGORY is specified, the query() only returns matching records that are associated with the specified data categories and are visible to the user. If unspecified, the query() returns the matching records that are visible to the user.
fieldGroupByListSpecifies a list of one or more fields, separated by commas, that are used to group the query results. A GROUP BY clause is used with aggregate functions to summarize the data. This clause also lets you roll up query results rather than having to process the individual records in your code.
fieldSubtotalGroupByListSpecifies a list of up to three fields, separated by commas, that are used to group the query results. The results include extra subtotal rows for the grouped data.
havingConditionExpressionIf the query includes a GROUP BY clause, this conditional expression filters the records that the GROUP BY returns.
fieldOrderByListSpecifies a list of one or more fields, separated by commas, that are used to order the query results.

4. Frequently Used SOQL Queries

Query Multi-Select Picklists

Filter values where Telecom or Retail is Selected

SELECT Id, Name FROM Account WHERE Industry__c includes ('Telecom','Retail')

Filter Values where both Telecom and Retail are Selected

SELECT Id, Name FROM Account WHERE Industry__c includes ('Telecom;Retail')

Get RecordType Id

Get Id and Name of Account RecordTypes

SELECT Id, Name FROM RecordType WHERE sObjectType = 'Account'

You can also get RecordType Id in Apex from Schema. Example: Below code will give Id of Customer RecordType of Account Object

Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer').getRecordTypeId();

Get Queue Id

Get Id of Queue with Name ‘Priority’ and Developer Name ‘Priority_Queue’

SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'Priority'

or 

SELECT Id FROM Group WHERE Type = 'Queue' AND DeveloperName = 'Priority_Queue'

Get Public Group Id

Get Id of Public Group with Name ‘CA Admins’ and Developer Name ‘CA_Admins’

SELECT Id FROM Group WHERE Type = 'Group' AND Name = 'CA Admins'

or 

SELECT Id FROM Group WHERE Type = 'Group' AND DeveloperName = 'CA_Admins'

Use LIKE

The LIKE operator in SOQL and SOSL provides a mechanism for matching partial text strings and includes support for wildcards. The % and _ wildcards are supported for the LIKE operator.

  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character

Get all Accounts with Name starting with DY

SELECT Id, Name FROM Account WHERE Name LIKE 'DY%'

Use Date Literals

Select all Account created yesterday

SELECT Id FROM Account WHERE
 CreatedDate = YESTERDAY

Select all Account created in last 90 days

SELECT Id FROM Account WHERE
 CreatedDate = LAST_90_DAYS
// This includes current day

Select all Opportunities closing in next 15 days

SELECT Id FROM Opportunity WHERE
 CloseDate > NEXT_N_DAYS:15

// This does not include current day

Query Code Coverage

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY ApexClassOrTrigger.Name ASC

Additional Information

This article will be updated soon with more frequently used SOQL Queries!


Share this article...

Please Leave a Comment

error: Content is protected !!

Discover more from DYDC

Subscribe now to keep reading and get access to the full archive.

Continue reading