Coding-With-The-Force / Salesforce-Separation-Of-Concerns-And-The-Apex-Common-Library

This repo hopes to better explain Separation of Concerns for Salesforce and how to leverage the Apex Common library to employ it in your org.
88 stars 19 forks source link

Sample code to query related records //Suggested Wiki Update #8

Open bdJohnson72 opened 1 year ago

bdJohnson72 commented 1 year ago

If we need our query to include related record data we create an instance of our parent selector. In this case, Accounts and provide the query factory with the relationship.

 public List<Contact> selectWithAccountData(){
        fflib_QueryFactory contactQueryFactory = newQueryFactory();
        new AccountSelector().configureQueryFactoryFields(contactQueryFactory,
                Contact.AccountId.getDescribe().getRelationshipName());

        return Database.query(contactQueryFactory.toSOQL());
    }

Running this query will generate the following query string: SELECT Account.AccountNumber, Account.Id, Account.Industry, Account.Name, Account.NumberOfEmployees, Account.Rating, AccountId, Email, Id, Name, Phone, Title FROM Contact ORDER BY Name ASC NULLS FIRST