//_blog

Brewtiful Beans Learns AT4DX, Part 1: One Selector, Three Teams

Last post introduced the four fflib roles: Selector, Domain, Service, and Unit of Work. This time, we’re looking at what happens when multiple teams work with the same SObject in one Salesforce org, and how AT4DX lets them share a selector without stepping on each other’s toes.

After this post, you can:

  • Explain why Brewtiful Beans has one AccountsSelector, owned by Org Common.
  • Understand how fieldset inclusion and selector method injection let each app extend the shared selector.
  • Recognize why directly constructing a selector creates a dependency, even when using an interface.

One Account, Three Teams

Sales, Marketing, and Service all work with Account records. Naturally, they all need to query Accounts, and some of those queries overlap. Everyone might need to find Accounts by name, for example.

Joe, being a thoughtful developer, creates a single AccountsSelector in Org Common that all three teams can use. One selector, one place for shared Account queries. Beautiful!

The very next day, Sales comes knocking.

They want to offer discounts to Accounts with more than 100 employees that have been customers for at least 90 days. They also need the Account’s Assigned Sales Rep, a custom field belonging to the Sales app.

Easy peasy. Joe adds a method and the additional field to AccountsSelector.

Then his Slack lights up.

Marketing: “Hey Joe, the AccountsSelector is broken. We don’t have that Assigned Sales Rep field in our app.”

Five minutes later:

Service: “Hey Joe, can you add a method to find Accounts with more than five open Complaint Cases?”

Joe’s beautiful shared selector is becoming a dumping ground for application-specific logic. Worse, the Sales team’s changes have broken Marketing’s app because the shared selector now references a field that Marketing doesn’t have.

Time for a brew break. There has to be a better way.

One Selector, Many Flavors

And indeed there is!

With AT4DX, each app can extend the shared AccountsSelector without modifying it. Two mechanisms make this possible:

  1. Fieldset inclusion lets applications add their own fields to the selector’s queries.
  2. Selector method injection lets applications provide their own query methods.

Both use a technique called dependency injection.

That sounds fancy, but anyone who’s ordered a latte at an American coffee shop is already familiar with the idea.

Think of the AccountsSelector as a regular latte: espresso and steamed milk. It’s the standard recipe everyone knows and loves.

But not everyone wants a plain latte. Some people want vanilla. Others want pumpkin spice. And the occasional customer wants something so elaborate Joe wonders whether it’s still coffee.

The barista doesn’t need a completely different coffee machine for every drink. They start with the same base and customize it for each order.

That’s the idea behind injection. Our shared selector provides the base, and each application supplies the ingredients it needs.

Fieldset Inclusion: Adding Ingredients

Let’s start with Sales and its Assigned Sales Rep field.

Rather than adding that field directly to the Org Common selector, Sales defines a fieldset containing the additional fields it needs.

AT4DX can include fields from registered fieldsets in the shared selector’s queries. The selector doesn’t need to know the names of those fields at compile time.

Back to our latte: the espresso and milk haven’t changed. Sales just asks for an extra shot of vanilla.

The important part is that Org Common never needs a reference to the Sales-specific field. Marketing and Service can continue using the shared selector without depending on Sales metadata.

The Sales team owns its fields. Org Common owns the selector. Everyone gets the Accounts they need.

Of course, adding fields isn’t enough when an application needs an entirely new way to find Accounts.

Selector Method Injection: New Recipes

Remember Service’s request to find Accounts with more than five open Complaint Cases?

That’s not just an additional field. It’s a new query with application-specific filtering logic.

We could add another method directly to AccountsSelector, but then Org Common would need to know about Service’s Complaint Case implementation. Before long, we’d be right back where we started.

Instead, Service can provide its own selector method and ask the shared selector to execute it.

For example, Service might have a method like this:

AccountsSelector.newInstance().selectInjection(
    SelectAccountsWithLotsOfComplaints.class,
    params
);

Here, Service supplies the query implementation and its parameters. The shared selector handles the injection without needing to know the specifics of complaints cases.

Think of this as handing the barista a recipe for your favorite seasonal latte. The coffee machine hasn’t changed, but it can now make something that wasn’t on the original menu.

With selector method injection, applications can introduce specialized queries without adding methods to the shared AccountsSelector or creating their own competing selectors.

We’ll explore the mechanics of fieldset inclusion and selector method injection in later posts. For now, the important concept is that each app supplies its own behavior without changing the shared foundation.

Keeping the Kitchen Organized

Let’s see where that leaves Brewtiful Beans.

Org Common owns the AccountsSelector and its interface, IAccountsSelector. It contains the common Account queries that everyone can use.

Each application owns its specializations.

Package Responsibility
Org Common Shared AccountsSelector, its interface, and common Account queries
Sales Assigned Sales Rep fields and employee-size queries
Marketing Engagement scoring queries
Service Complaint Case queries

None of the application packages needs to modify or subclass the shared selector.

More importantly, Org Common doesn’t need to know about the application-specific fields or methods. The dependency goes in one direction: applications depend on Org Common, not the other way around.

This is especially useful in a large Salesforce org where multiple teams develop and deploy their applications independently.

There’s still just one selector for Account. It simply supports different flavors.

Joe’s feeling pretty good about himself. Three teams, one selector, and no more fighting over whose custom field broke whose application.

The Typing Tax: This architecture involves some upfront ceremony: interfaces, selector classes, factory methods, configuration, and mocks. For one SObject, that might feel excessive. For twelve SObjects across three teams, the consistency starts paying off. And in Part 10, we’ll look at how to automate much of that boilerplate.

Check Your Brew

1. Why shouldn’t Sales add its Assigned Sales Rep field directly to the Org Common selector?

  • A. Custom fields cannot be queried by selectors.
  • B. It creates a dependency on Sales-specific metadata that other applications might not have.
  • C. AT4DX only allows standard fields in shared selectors.
  • D. It makes Account queries slower.
Reveal the answer

B. Org Common should not depend on metadata owned by individual applications. Fieldset inclusion lets Sales add the fields it needs without changing the shared selector.

2. Service needs a specialized query for Accounts with more than five open Complaint Cases. Which approach fits AT4DX?

  • A. Create a separate ServiceAccountsSelector.
  • B. Add the query directly to Org Common.
  • C. Use selector method injection to supply the Service-specific query.
  • D. Query every Account and filter the results in Apex.
Reveal the answer

C. Selector method injection lets Service supply its own query implementation without modifying or subclassing the shared selector.

**Next up: Part 2 — The Hidden Dependency.** We'll look more closely at `new` versus `newInstance()`, why factories matter, and the four AT4DX factory boundaries.