1

I'm working on a skipe for this fflib and I'm facing this error when I try to execute a Cross-Object or Sub-Select query:.

|WARN|fflib_QueryFactory.subselectQuery(Schema.SObjectType) is deprecated and will be removed in a future release. Use fflib_QueryFactory.subselectQuery(String) or fflib_QueryFactory.subselectQuery(ChildRelationship) instead.

I'm basically following the Trailhead: Apex Enterprise Patterns: Domain & Selector Layers.

On the last section of this module, Apply Selector Layer Principles in Apex there is a part explaining how to reuse field list: Advanced: Reusing Field Lists for Cross-Object and Sub-Select Queries

public List<Opportunity> selectByIdWithProducts(Set<ID> idSet) {

    // Query Factory for this Selector (Opportunity)
    fflib_QueryFactory opportunitiesQueryFactory = newQueryFactory();

    // Add a query sub-select via the Query Factory for the Opportunity Products
    fflib_QueryFactory lineItemsQueryFactory =
        new OpportunityLineItemsSelector().
            addQueryFactorySubselect(opportunitiesQueryFactory);

    // Add cross object query fields for Pricebook Entry, Products and Pricebook
    new PricebookEntriesSelector().
        configureQueryFactoryFields(lineItemsQueryFactory, 'PricebookEntry');
    new ProductsSelector().
        configureQueryFactoryFields(lineItemsQueryFactory, 'PricebookEntry.Product2');
    new PricebooksSelector().
        configureQueryFactoryFields(lineItemsQueryFactory, 'PricebookEntry.Pricebook2');

    // Set the condition and build the query
    return (List<Opportunity>) Database.query(
        opportunitiesQueryFactory.setCondition('id in :idSet').toSOQL());
}

When executing this method I get the error mentioned on the top of this question. I have tried several things but I still get the same error. All the test code is updated from classes on this repository. Anyone has ever faced similar issue with this library?

1 Answer 1

1

This is what I would do ... (some variable renaming to make it more compact)

public List<Opportunity> selectByIdWithProducts(Set<ID> ids) {

    if (ids.isEmpty()) {return new List<Opportunity>();}  // avoid burning soql
    // Query Factory for this Selector (Opportunity)
    fflib_QueryFactory oQF = newQueryFactory()
                               .setCondition('Id IN : ids');

    // Add a query sub-select via the Query Factory for the Opportunity Products
    fflib_QueryFactory oliQF = oQF.subSelectQuery('OpportunityLineItems')
                                .selectFields(new OpportunityLineItemsSelector()
                                                  .getSObjectFieldList());


    // Add cross object query fields for Pricebook Entry, Products and Pricebook
    new PricebookEntriesSelector().
        configureQueryFactoryFields(oliQF, 'PricebookEntry');
    new ProductsSelector().
        configureQueryFactoryFields(oliQF, 'PricebookEntry.Product2');
    new PricebooksSelector().
        configureQueryFactoryFields(oliQF, 'PricebookEntry.Pricebook2');

    // Set the condition and build the query
    return  Database.query(oQF.toSOQL());
}

The key is here:

// Add a query sub-select via the Query Factory for the Opportunity Products
    fflib_QueryFactory oliQF = oQF.subSelectQuery('OpportunityLineItems')
                                .selectFields(new OpportunityLineItemsSelector()
                                                  .getSObjectFieldList());

Use of the relationship name in the argument avoids the deprecation error you see

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .