Skip to main content
[ This file is autogenerated from apexdoc ]

AqSoql

Builder class for SOQL quieries

Group apex/core

Constructors​

public AqSoql(SObjectType objType)​

public AqSoql(String objTypeName)​

public AqSoql(SObjectField lookupField, SObjectType childObjectType)​

Constructor for sub-select queries

Parameters​

ParamDescription
lookupFielda lookup SObjectField
childObjectTypeSObjectType for a child

Example​

List<String> FIELDS_FOR_SELECTION = {'Name'};
Integer SEARCH_RESULTS_LIMIT = 1000;
public static List<Account> searchAccounts(String name) {
String condition = 'Name = :name';
List<String> orderFields = { 'Name' };
AqSoql aqSoql = new AqSoql(Account.getSObjectType())
.withColumns(FIELDS_FOR_SELECTION)
.withWhere(condition)
.withOrderBy(orderFields)
.withRowLimit(SEARCH_RESULTS_LIMIT);
return Database.query(aqSoql.getQueryString());
}

public AqSoql(AqSoql soql)​


Methods​

public AqSoql withColumns(List<String> cols)​

Set columns list you want to select

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
.withColumns(columnsToSelect);
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withAggregation(List<AqAggregation> aggregations)​

Use to construct query with aggregations

Example​

AqAggregation ag = AqAggregation.max(AqTestObject__c.Date__c, 'alias');
// queries the Maximum value of 'Date__c' field of 'AqTestObject__c' object
AqSoql soql = new AqSoql(obj)
.withAggregation(new List<AqAggregation> { ag });
List<AggregateResult> results = soql.aggregate();

public AqSoql withGroupBy(List<String> gb)​

Set GROUP BY columns for Aggregation querries only.

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
List<String> groubBy = {'Name'};
AqAggregation ag = AqAggregation.max(AqTestObject__c.Date__c, 'alias');
AqSoql soql = new AqSoql(obj)
.withColumns(columnsToSelect)
.withAggregation(new List<AqAggregation> { ag })
.withGroupBy(groubBy); // groups results by 'Name' field
List<AggregateResult> results = soql.aggregate();

public AqSoql withWhere(String wc)​

Set WHERE condition to query

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
// returns records Where Name is not Bull
AqSoql soql = new AqSoql(obj)
.withWhere('Name != NULL')
.withColumns(columnsToSelect);
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withSubSelects(List<AqSoql> ss)​

Add sub query to your query

Example​

AqSoql subQuery = new AqSoql('Relation__r')
.withColumns('Price');
List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
.withColumns(columnsToSelect)
// queries each object record with 'Relation__c' related records list
.withSubSelects(new List<AqSoql>{subQuery});
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withRowLimit(Integer rl)​

Set query records LIMIT

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
.withColumns(columnsToSelect)
// selects only first 1000 records
.withRowLimit(1000);
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withOrderBy(List<String> ob)​

Set ORDER BY parameter to query

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
// displays records odered by 'Name' field and Descending
.withOrderBy(new List<String>{ 'Name DESC' })
.withColumns(columnsToSelect);
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withoutSecurityEnforced()​

Use it to omit WITH SECURITY_ENFORCED and WITH USER_MODE settings. Default value is WITH SECURITY_ENFORCED

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
// displays records odered by 'Name' field and Descending
.withOrderBy(new List<String>{ 'Name DESC' })
.withColumns(columnsToSelect)
.withoutSecurityEnforced();
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withSecurityEnforced()​

Use it to set security level to WITH SECURITY_ENFORCED. Turned on by default.

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
// displays records odered by 'Name' field and Descending
.withOrderBy(new List<String>{ 'Name DESC' })
.withColumns(columnsToSelect)
.withSecurityEnforced();
List<Object> result = Database.query(soql.getQueryString());

public AqSoql withUserMode()​

Use it to set security level to WITH USER_MODE which enforces sharing regardless of class declaration.

Example​

List<String> columnsToSelect = {'Name', 'Address__c'};
AqSoql soql = new AqSoql(obj)
.withColumns(columnsToSelect)
.withUserMode();
try {
List<Object> result = Database.query(soql.getQueryString());
} catch (QueryException ex) {
throw new Aq.QueryPermissionException(ex);
}

public String getQueryString()​

Use it as a final builder method to return assembled SOQL query

Example​

AqSoql soql = new AqSoql(obj)
.withColumns(new List<String>{ 'Name' });
String soqlQuery = soql.getQueryString();
List<sObjects> records = Database.query(soqlQuery);

public List<AggregateResult> aggregate()​

Use it as a final method for aggregation querries

Example​

AqAggregation ag = AqAggregation.max(AqTestObject__c.Date__c, 'alias');
AqSoql soql = new AqSoql(obj)
.withAggregation(new List<AqAggregation> { ag });
List<AggregateResult> results = soql.aggregate();

public Integer countQuery()​

Use it as a final method for COUNT querries

Example​

AqSoql soql = new AqSoql(obj)
.withWhere('RequiredText__c != NULL);
// counts all records instead of quering it
Integer numberOfRecords = soql.countQuery();

public static String getBaseSoql(SObjectType objectType, Set<String> columns)​

Assemble SOQL string for a given SObjectType and columns set

Example​

Set<String> columnsToSelect = {'Name', 'Address__c'};
String soqlQuery = AqSoql.getBaseSoql(obj, columnsToSelect);
List<sObjects> records = Database.query(soqlQuery);

public static String getBaseSoql(SObjectType objectType, List<String> columns, List<AqSoql> subSelects)​

Assemble SOQL string for a given SObjectType, columns set and sub queries list

Example​

AqSoql subQuery = new AqSoql('Relation__r')
.withColumns('Price');
List<AqSoql> subQueries = new List<AqSoql>{ subQuery };
Set<String> columnsToSelect = {'Name', 'Address__c'};
String soqlQuery = AqSoql.getBaseSoql(obj, columnsToSelect, subQueries)