Brewtiful Beans Learns AT4DX, Part 6: Custom Brews
Marketing needs to find Accounts by slogan. Adding a field to the shared selector’s results won’t answer that question: the team needs a new query.
Joe can keep the shared selector and let Marketing supply the recipe. Selector method injection puts the specialized query in Marketing’s package while using the shared selector’s query machinery.
After this post, you can:
- Write an injectable query class that extends
AbstractSelectorMethodInjectable. - Pass typed parameters into an injectable through
selectInjection(...). - Decide between a shared selector method, method injection, and a second selector, and choose correctly.
Why Not Add It to the Shared Selector?
Marketing needs Accounts by slogan. The field Slogan__c lives in sales-marketing-common, not in Org Common. Two tempting options both go wrong:
- Add
selectBySlogantoAccountsSelector. Now Org Common depends on Marketing’s schema. The shared class compiles against something one team owns. - Create a
MarketingAccountsSelector. Now there are two Account selectors, and the common behavior gets copied and drifts.
The right answer keeps Org Common singular and puts Marketing’s query where Marketing can own it.
The Recipe Card
Marketing writes a small class that owns its query. It lives in marketing-app:
// Package: marketing-app
public inherited sharing class SelectBySloganSelectorMethod
extends AbstractSelectorMethodInjectable
implements ISelectorMethodInjectable {
public List<SObject> selectQuery() {
Parameters params = (Parameters) getParameters();
Set<String> slogans = params.slogans;
return Database.query(
newQueryFactory()
.setCondition(Account.Slogan__c + ' IN :slogans')
.toSOQL()
);
}
public class Parameters implements ISelectorMethodParameterable {
public Set<String> slogans;
}
}
Three things make this work:
newQueryFactory()comes from the machine. It starts with the Account selector’s common fields and includes any fieldsets from Part 5. The injectable doesn’t rebuild the field list.getParameters()returns the typed recipe inputs. TheParametersinner class is the only way values get in.selectQuery()returns the result. The injectable runs the query and hands back records.
Marketing calls it through the singular selector:
SelectBySloganSelectorMethod.Parameters params =
new SelectBySloganSelectorMethod.Parameters();
params.slogans = new Set<String>{ 'Customer first', 'Built to last' };
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectBySloganSelectorMethod.class,
params
);
Look at what the caller names. It names the injectable class and the parameters, not new AccountsSelector() or a Marketing selector. AccountsSelector stays the one Account selector in the org.
The caller passes the class explicitly; there is no selector-method binding record. AT4DX constructs the injectable, supplies its parameters and query factory, and calls selectQuery().
The QueryLocator Variant
Batch Apex needs a Database.QueryLocator instead of a list. The pattern is parallel:
public inherited sharing class SelectStrategicAccountsQueryLocatorMethod
extends AbstractSelectorQueryLocatorInjectable
implements ISelectorQueryLocatorMethodInjectable {
public Database.QueryLocator selectQueryLocator() {
Parameters params = (Parameters) getParameters();
Decimal minimumRevenue = params.minimumRevenue;
return Database.getQueryLocator(
newQueryFactory()
.setCondition(Account.AnnualRevenue + ' >= :minimumRevenue')
.toSOQL()
);
}
public class Parameters implements ISelectorMethodParameterable {
public Decimal minimumRevenue;
}
}
The caller uses the matching method:
SelectStrategicAccountsQueryLocatorMethod.Parameters params =
new SelectStrategicAccountsQueryLocatorMethod.Parameters();
params.minimumRevenue = 1000000;
Database.QueryLocator locator =
AccountsSelector.newInstance().selectQueryLocatorInjection(
SelectStrategicAccountsQueryLocatorMethod.class,
params
);
The list and QueryLocator versions differ in two places: the base class, and the method name. Keep the pairing straight, because the compiler error for a mismatch is not friendly.
Rules for Good Recipes
- Use Sets for
INfilters.Set<String>works directly withIN :slogans. - Use bind variables. Never concatenate values into SOQL, especially values that came from users.
- Keep selector methods read-only. No DML, no service calls, no business actions. A recipe fetches the beans and nothing more.
- Pass null only for a parameterless injectable. AT4DX doesn’t call
setParameters(...)when the argument is null, sogetParameters()returns null. Casting null in Apex is fine. Readingparams.sloganson it throws aNullPointerException.
Keep the injectable in Marketing while only Marketing needs it. If Sales needs the same query, the teams can move it to their shared package. Use bind variables for values, and pass a parameter object whenever the injectable reads one. A null argument leaves getParameters() null.
Tests can mock the selector at the service boundary; test the injectable with a real query when its database behavior matters.
Check Your Brew
1. Where should a query that only Marketing needs live?
- A. In an injectable class in the Marketing package, called through
selectInjection(...) - B. In a
MarketingAccountsSelectorsubclass - C. In
AccountsSelector, in Org Common - D. In the Marketing trigger
Reveal the answer
A. The injectable keeps Marketing’s query in Marketing’s package, and the singular selector stays in Org Common. No second selector is created.
2. What does newQueryFactory() give the injectable?
- A. An empty query with no fields
- B. A cached list of Accounts
- C. A query factory that already includes the Account selector’s common fields and any injected fieldsets
- D. A DML handle for updates
Reveal the answer
C. The factory starts from the selector’s common fields and includes fieldset injection. The injectable adds its condition on top.
3. A caller passes null as the parameters to an injectable whose selectQuery() reads getParameters(). What happens?
- A. The query runs with every Account
- B. Reading a field such as
params.slogansthrows aNullPointerException - C. AT4DX builds a default
Parametersobject - D. The injectable is skipped without an error
Reveal the answer
B. AT4DX doesn’t call setParameters(...) when the argument is null, so getParameters() returns null. The first read of params.slogans throws a NullPointerException. Pass a real Parameters object.
Next up: Part 7, Criteria and Actions, is the heaviest post in the series. It covers how Sales and Marketing add behavior to one empty AccountDomain without subclassing it. Strong coffee recommended.