Skip to content

v4.2.0

Latest
Compare
Choose a tag to compare
@pgajek2 pgajek2 released this 03 Feb 18:19
7e0c67f

03-February-2025

Scope

SOQL

  • Queryable toLabel(SObjectField field, String alias);
  • Queryable toLabel(String field, String alias);
  • Queryable sort(String direction);
  • Map<String, SObject> toMap(String relationshipName, SObjectField targetKeyField);
  • Map<String, List<SObject>> toAggregatedMap(String relationshipName, SObjectField targetKeyField);
  • Issued queries counter
  • Code refactoring

SOQL.SubQuery

  • SubQuery with(String fields);
  • SubQuery orderBy(String field);
  • SubQuery sort(String direction);

SOQL.FilterGroup

  • FilterGroup add(List<Filter> filters)
  • FilterGroup add(List<String> dynamicConditions)

SOQLCache

  • removeFromCache(List<SObject> records)

SOQL

toLabel(SObjectField field, String alias)

Enhancement: #147

You can specify the toLabel(field) alias to avoid the error: “duplicate field selected: field”.

Signature

Queryable toLabel(SObjectField field, String alias);
Queryable toLabel(String field, String alias);

Example

SELECT Status, toLabel(Status) leadStatus FROM Lead
SOQL.of(Lead.SObjectType)
    .with(Lead.Status)
    .toLabel(Lead.Status, 'leadStatus')
    .toList();
SELECT Company, toLabel(Recordtype.Name) recordTypeName FROM Lead
SOQL.of(Lead.SObjectType)
    .with(Lead.Company)
    .toLabel('Recordtype.Name', 'recordTypeName')
    .toList();

Map<String, SObject> toMap(String relationshipName, SObjectField targetKeyField)

Enhancement: #144

Signature

Map<String, SObject> toMap(String relationshipName, SObjectField targetKeyField);

Example

Map<String, Account> parentCreatedByEmailToAccount = new Map<String, Account>();

for (Account account : [SELECT Id, Parent.CreatedBy.Email FROM Account]) {
   parentCreatedByEmailToAccount.put(account.Parent.CreatedBy.Email, account);
}

Map<String, Account> parentCreatedByEmailToAccount = 
   (Map<String, Account>) SOQL.of(Account.SObjectType).toMap('Parent.CreatedBy', User.Email);

Map<String, List<SObject>> toAggregatedMap(String relationshipName, SObjectField targetKeyField);

Enhancement: #144

Signature

Map<String, List<SObject>> toAggregatedMap(String relationshipName, SObjectField targetKeyField);

Example

Map<String, List<Account>> parentCreatedByEmailToAccount = new Map<String, List<Account>>();

for (Account account : [SELECT Id, Parent.CreatedBy.Email FROM Account]) {
   if (!parentCreatedByEmailToAccount.containsKey(account.Parent.CreatedBy.Email)) {
      parentCreatedByEmailToAccount.put(account.Parent.CreatedBy.Email, new List<Account>());
   }
   
   parentCreatedByEmailToAccount.get(account.Parent.CreatedBy.Email).add(account);
}

Map<String, List<Account>> parentCreatedByEmailToAccounts = 
   (Map<String, List<Account>>) SOQL.of(Account.SObjectType).toAggregatedMap('Parent.CreatedBy', User.Email);

Issued queries counter

Mocked queries are now counted by SOQL Lib, and if the number of issued queries exceeds 100, a new QueryException('Too many SOQL queries.') will be thrown. This allows you to catch the issue in your unit tests.

SOQL.SubQuery

Thanks, @salberski for the collaboration!

SubQuery with(String fields);

Signature

SubQuery with(String fields)

Example

SELECT Id, (
    SELECT Id, Name, Phone, RecordTypeId, Title, Salutation
    FROM Contacts
) FROM Account
SOQL.of(Account.SObjectType)
    .with(SOQL.SubQuery.of('Contacts')
        .with('Id, Name, Phone, RecordTypeId, Title, Salutation')
    )
    .toList();

SubQuery orderBy(String field);

Signature

SubQuery orderBy(String field)

Example

SELECT Id, (
    SELECT Id
    FROM Contacts
    ORDER BY Name
) FROM Account
SOQL.of(Account.SObjectType)
    .with(SOQL.SubQuery.of('Contacts')
        .orderBy('Name')
    )
    .toList();

SubQuery sort(String direction);

Signature

SubQuery sort(String direction)

Example

SELECT Id, (
    SELECT Id
    FROM Contacts
    ORDER BY Name DESC
) FROM Account
SOQL.of(Account.SObjectType)
    .with(SOQL.SubQuery.of('Contacts')
        .orderBy('Name')
        .sort('DESC')
    )
    .toList();

SOQL.FilterGroup

Thanks, @salberski for the collaboration!

Signature

FilterGroup add(List<Filter> filters);
FilterGroup add(List<String> dynamicConditions);

Example

// SELECT Id FROM Account WHERE (Name = 'Test' AND BillingCity = 'Krakow')

SOQL.of(Account.SObjectType)
    .whereAre(SOQL.FilterGroup
        .add(new List<SOQL.Filter> {
            SOQL.Filter.with(Account.Name).equal('Test'),
            SOQL.Filter.with(Account.BillingCity).equal('Krakow')
        })
    ).toList();
// SELECT Id FROM Account WHERE (Name = 'Test' AND BillingCity = 'Krakow')

SOQL.of(Account.SObjectType)
    .whereAre(SOQL.FilterGroup
        .add(new List<String> {
            'Name = \'Test\'',
            'BillingCity = \'Krakow\''
        })
    ).toList();

SOQLCache

removeFromCache(List<SObject> records)

The removeFromCache method clears records from the cache, triggering an automatic refresh the next time the query is executed.

Thanks, @patrykacc for the collaboration!

Signature

Cacheable removeFromCache(List<SObject> records);

Example

trigger SomeObjectTrigger on SomeObject (after update, after delete) {
    SOQLCache.removeFromCache(Trigger.new);
}

New Contributors