A custom Selector method with related fields

In addition to querying child records, related records can also be queried using the SOQL field dot notation. This provides an additional way to optimize the querying of additional record information, along with having to make an additional query.

The following is an example of a custom Selector method for the ContestantsSelector class, where Contestant records are queried along with the related Driver record fields. The resulting Contestant__c objects expose the Driver__r field, which provides an instance of Driver__c populated with the Driver__c fields specified in DriversSelector:

public List<Contestant__c>selectByIdWithDriver(Set<Id>driverIds) { 
  fflib_QueryFactorycontestantFactory = newQueryFactory(); 
 
  new DriversSelector(). 
    configureQueryFactoryFields( 
    contestantFactory,  
    Contestant__c.Driver__c.getDescribe().getRelationshipName()); 
 
  return Database.query( 
    contestantFactory.setCondition('Id in :driverIds').toSOQL()); 
} 

The fflib_SObjectSelector.configureQueryFactoryFields method adds the Selector fields to the query factory passed as the first parameter, utilizing the relationship prefix specified in the second parameter.

Due to the reuse of DriversSelector in the ContestantsSelector classes method in the preceding code, the examples in the next code block return consistently populated Driver__c records. This is important to the logic contained within the Domain class's Drivers.verifyCompliance method. This flexibility allows for the most optimal way of querying Driver__c records and safer reuse of the Domain class method regardless of how the records were queried.

This first example constructs the Drivers Domain class with results from the standard selectById method on the DriversSelector class:

List<Driver__c> drivers = 
new DriversSelector().selectById(driverIds); List<ComplianceService.VerifyResult> results = new Drivers(drivers).verifyCompliance();

This second example achieves the same, but queries the Drivers records via the custom Selector method, selectByIdWithDriver, on the ContestantsSelector class:

List<Driver__c> drivers = new List<Driver__c>(); 
List<Contestant__c> contestants = 
new ContestantsSelector().selectByIdWithDriver(contIds); for(Contestant__c contestant : contestants) drivers.add(contestant.Driver__r); List<ComplianceService.VerifyResult> results = new Drivers(drivers).verifyCompliance();

Again, both of these examples have shown that even when using related fields in SOQL, the resulting records can be kept in alignment with the desired fields as expressed by the selectors. The selectByIdWithDriver method generates the following SOQL:

SELECT        
  Qualification1LapTime__c, ChampionshipPoints__c, Driver__c, 
  GridPosition__c, Qualification3LapTime__c, RacePosition__c, 
  Name, DNF__c, RaceTime__c, Id, DriverRace__c, 
  Qualification2LapTime__c, Race__c, 
  Driver__r.FIASuperLicense__c, Driver__r.Name, 
  Driver__r.Id, Driver__r.Team__c 
 FROM Contestant__c 
 WHERE Id in :driverIds 
 ORDER BY Name 

This section illustrated how you can include the driver-related fields when querying the contestants. If you require more granular control over specific fields from different related objects, you can use a custom dataset to express this information in a clear way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.142.56