|
1 | | -# Apex Query Generator |
2 | | -<a target="_blank" href="https://githubsfdeploy.herokuapp.com"> |
3 | | - <img alt="Deploy to Salesforce" |
4 | | - src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/src/main/webapp/resources/img/deploy.png"> |
5 | | -</a> |
6 | | - |
7 | | -## Overview |
8 | | -This is an freestanding version of the [Nebula framework's](https://github.com/jongpie/NebulaFramework/) query engine - it has been updated to remove any dependencies on the rest of the Nebula framework. |
9 | | - |
10 | | -## Features |
11 | | -The overall goal of the project is to generate dynamic SOQL & SOSL queries. Features currently include |
12 | | -* Leverage field-level security to dynamically include fields |
13 | | -* Dynamically include filter conditions (not possible with standard SOQL/SOSL) |
14 | | -* Retain Salesforce's compilation-time errors for invalid fields while still taking advantage of dynamic queries - this helps avoid issues with deleting fields, misspelled field names, etc that can occur when working with strings and dynamic queries |
15 | | -* Support for nearly all SOQL & SOSL features & keywords, including date literals, aggregate results and more |
16 | | -* Easy-to-use query caching |
| 1 | +# Apex Dynamic SOQL & SOSL Library |
| 2 | +A lightweight Apex library for easily building dynamic SOQL queries & SOSL searches<br /><br /> |
| 3 | +<a href="https://githubsfdeploy.herokuapp.com" target="_blank"> |
| 4 | + <img alt="Deploy to Salesforce" src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png"> |
| 5 | +</a> |
| 6 | + |
| 7 | +## Features |
| 8 | +* Easily add a field if the field meets the category specified, using the Soql.FieldCategory enum |
| 9 | +* Easily add any fields that are accessible, updateable, standard or custom, using the Soql.FieldCategory enum |
| 10 | +* Easily add fields from a field set |
| 11 | +* Automatically adds the parent name field for any lookup/master-detail fields |
| 12 | +* Adds translations for picklist fields & record types by calling includeLabels() |
| 13 | +* Adds localized formatting for number, date, datetime, time, or currency fields by calling includeFormattedValues() |
| 14 | +* Leverage query scope to filter results |
| 15 | +* Reuse your dynamic SOQL queries to quickly build dynamic SOSL searches |
| 16 | + |
| 17 | +## SOQL Query Examples |
| 18 | +**Basic Usage:** Query an object & return the object's ID and display name field (typically the 'Name' field, but some objects use other fields, like Task.Subject and Case.CaseNumber). Since no filters have been added, this query would also return all accounts. |
| 19 | + |
| 20 | +``` |
| 21 | +List<Account> accounts = new Soql(Schema.Account.SobjectType).getQueryResults(); |
| 22 | +``` |
| 23 | + |
| 24 | +**Advanced Usage:** Query an object & leverage the query builder methods. The order of the builder methods does not matter - you can arrange the calls to these methods in any order that you prefer. |
| 25 | + |
| 26 | +``` |
| 27 | +Soql accountQuery = new Soql(Schema.Account) // Query the account object |
| 28 | + .addField(Schema.Account.ParentId) // Include the ParentId field, using SObjectField. The current user must have at least read access to the field |
| 29 | + .addField(Schema.Account.Type, Soql.FieldCategory.UPDATEABLE) // Include the Type field if the current user has access to update it |
| 30 | + .addFields(Soql.FieldCategory.CUSTOM) // Include all custom fields - Soql.cls only includes fields that are accessible to the user |
| 31 | + .addFields(myAccountFieldSet) // Include all fields in a field set that are accessible to the user |
| 32 | + .removeField(Schema.Account.My_custom_Field__c) // remove a custom field |
| 33 | + .usingScope(Soql.Scope.MINE) // Set the query scope |
| 34 | + .filterWhere(Schema.Account.CreatedDate, '=', new Soql.DateLiteral('LAST_WEEK')) // Filter on the created date, using a date literal |
| 35 | + .orderBy(Schema.Account.Type) // Order by a field API name - sort order/nulls defaults to 'Type ASC NULLS FIRST' |
| 36 | + .orderBy(Account.Name, Soql.SortOrder.ASCENDING) // Order by, using SObjectField & sort order |
| 37 | + .orderBy(Account.AnnualRevenue, Soql.SortOrder.DESCENDING, false) // Order by, using SObjectField, sort order and nulls sort order |
| 38 | + .limitCount(100) // Limit the results to 100 records |
| 39 | + .includeLabels() // Include labels/translations for any picklist fields or record types. These are aliased using the convention 'FieldName__c_Label' |
| 40 | + .includeFormattedValues() // Include formatted values for any number, date, time, or currency fields |
| 41 | + .cacheResults() // When enabled, the query results are internally cached - any subsequent calls for getQueryResults() will returned cached results instead of executing the query again |
| 42 | + .offset(25); // Skip the first 25 results |
| 43 | +
|
| 44 | +// Execute the query and store the results in the 'accounts' variable |
| 45 | +List<Account> accounts = accountQuery.getQueryResults(); |
| 46 | +``` |
| 47 | + |
| 48 | +## SOSL Search Examples |
| 49 | +**Basic Usage:** Search a single object |
| 50 | + |
| 51 | +``` |
| 52 | +Soql userQuery = new Soql(Schema.User.SobjectType); // Create an instance of Soql for an Sobject - you can include additional fields, filters, etc |
| 53 | +Sosl userSearch = new Sosl('my search term', userQuery); // Create a new Sosl instance with a search term & instance of Soql |
| 54 | +List<User> userSearchResults = userSearch.getFirstSearchResults(); // Sosl returns a list of lists of sobjects - getFirstSearchResults() returns the first list |
| 55 | +``` |
| 56 | + |
| 57 | +**Advanced Usage:** Search several objects |
| 58 | + |
| 59 | +``` |
| 60 | +Soql accountQuery = new Soql(Schema.Account.SobjectType); // Create an instance of Soql for the Account object |
| 61 | +Soql contactQuery = new Soql(Schema.Contact.SobjectType); // Create an instance of Soql for the Contact object |
| 62 | +Soql leadQuery = new Soql(Schema.Lead.SobjectType); // Create an instance of Soql for the Lead object |
| 63 | +List<Soql> queries = new List<Soql>{accountQuery, contactQuery, leadQuery}; // Add the Soql queries to a list |
| 64 | +
|
| 65 | +Sosl mySearch = new Sosl('my search term', queries); // Create a new Sosl instance with a search term & the list of Soql queries |
| 66 | +List<List<Sobject>> searchResults = mySearch.getSearchResults(); // Sosl returns a list of lists of sobjects - getFirstSearchResults() returns the first list |
| 67 | +``` |
0 commit comments