Skip to content

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

@bdJohnson72

Description

@bdJohnson72

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions