-
Notifications
You must be signed in to change notification settings - Fork 458
SOQLRecipes
Demonstrates how to make various types of SOQL calls including multi-object queries, and aggregate queries
Group Data Recipes
Demonstrates the proper way to query accounts with SOQL keeping FLS and CRUD in account.
public static List<Account> getRecords()
List<Account>
List<Account> results = SOQLRecipes.querySingleObject();
System.debug(results);
Demonstrates how to loop over a SOQL query
public static Integer getLargeNumberOfRecords()
Integer
System.debug(SOQLRecipes.getLargeNumberOfRecords());
One of the little known features of SOQL for loops is that you can iterate not only over each record returned by the query, but also over each chunk of records. As the code below demonstrates, specifying the iteration variable as a list/array will return 200 record chunks from the query, rather than individual records.
Note: Normally, if you're only dealing with counts of records, you'd utilize the Count() soql method, but in this case we're demonstrating that this form of a soql for loop gives you access both to a list of records, and to the records themselves.
public static Integer getChunksOfLargeNumbersOfRecords()
Integer
Demonstrates how to use a WHERE clause in a SOQL query
public static List<Account> getRecordsByFieldValue()
List<Account>
System.debug(SOQLRecipes.getRecordsByFieldValue());
Demonstrates how to use a complex WHERE clause in a SOQL query
public static List<Account> getRecordsByMultipleFieldValues()
List<Account>
System.debug(SOQLRecipes.getRecordsByMultipleFieldValues());
Demonstrates how to use the LIMIT clause in a SOQL statement
public static List<Account> getSpecificNumberOfRecords()
List<Account>
System.debug(SOQLRecipes.getSpecificNumberOfRecords());
Demonstrates how to use a bound variable to define the LIMIT
public static List<Account> getFirstXRecords(Integer wantedNumberOfRows)
Name | Type | Description |
---|---|---|
wantedNumberOfRows | Integer | the number of rows desired |
List<Account>
System.debug(SOQLRecipes.getFirstXRecords(5));
Demonstrates how to use a bound variable in a WHERE clause
public static List<Account> getAccountRecordsInState(String state)
Name | Type | Description |
---|---|---|
state | String | String representing a US State code (AK, KS, etc.) |
List<Account>
System.debug(SOQLRecipes.getAccountRecordsInState('ks'));
Demonstrates how to get a limited number of results with a given offset; Ie: get the second set of 10 records.
public static List<Account> getSecond10AccountRecords()
List<Account>
System.debug(SOQLRecipes.getSecond10AccountRecords());
Demonstrates how to query an object, as well as it's related child objects
public static List<Account> getRecordsWithRelatedRecords()
List<Account>
System.debug(SOQLRecipes.getRecordsWithRelatedRecords());
Demonstrates how to query fields from a parent object through the relationship field
public static List<Contact> getParentRecordDetailsFromChildRecord()
List<Contact>
System.debug(SOQLRecipes.getParentRecordDetailsFromChildRecord());
Demonstrates how to write a query that pulls information from two parent objects through a junction object
public static List<Junction__c> getDetailsFromBothParentRecords()
List<Junction__c>
System.debug(SOQLRecipes.getDetailsFromBothParentRecords());
demonstrates how to use aggregate methods, like Sum() or Count() in a SOQL query. This example generates the sum of opportunities associated with a specified Account
public static Double getSumOfOpportunityRecords(Id accountId)
Name | Type | Description |
---|---|---|
accountId | Id | an AccountId |
Double
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
System.debug(SOQLRecipes.getSumOfOpportunityRecords(accountId));