//_blog

Brewtiful Beans Learns AT4DX, Part 5: Adding Fields Without Stirring the Pot

Sales needs SalesStage__c when it queries Accounts. Joe could add the field to Org Common’s selector, but that would make the shared package depend on Sales metadata. Marketing and Service would inherit that dependency too.

Sales can instead add its ingredient to the shared latte: a fieldset tells the selector which extra fields to include.

After this post, you can:

  • Explain why adding a team’s field to getSObjectFieldList() breaks the package graph.
  • Define a fieldset and register it with SelectorConfig_FieldSetInclusion__mdt.
  • Check the deployed fieldset wiring with an integration test.

The Tempting Shortcut

The obvious fix is to add the field to the shared selector:

// Wrong: Org Common now depends on Sales-owned schema.
public override List<Schema.SObjectField> getSObjectFieldList() {
    return new List<Schema.SObjectField>{
        Account.Id,
        Account.Name,
        Account.SalesStage__c
    };
}

It compiles where the Sales field exists, but Org Common now depends on Sales metadata. Other packages can’t deploy that selector independently, and every team’s fields compete for space in the same class.

Creating a SalesAccountsSelector isn’t the answer either. Part 3 explained why AT4DX keeps one selector per SObject.

Add the Ingredients

Fieldset injection lets Sales add fields from its own package, and AccountsSelector never names them.

Sales defines a Salesforce fieldset in the package that owns those fields:

<?xml version="1.0" encoding="UTF-8"?>
<FieldSet xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>SalesAccountFields</fullName>
    <label>Sales Account Fields</label>
    <displayedFields><field>SalesStage__c</field></displayedFields>
    <displayedFields><field>ExpectedRevenue__c</field></displayedFields>
    <displayedFields><field>SalesRep__c</field></displayedFields>
</FieldSet>

Then it registers the fieldset with a binding record:

SelectorConfig_FieldSetInclusion__mdt
  BindingSObject__c: Account
  FieldsetName__c:   SalesAccountFields

When any caller runs AccountsSelector.newInstance().selectById(...), the selector loads every fieldset bound to Account and adds those fields to the query. Sales’ fields show up, and the selector code is unchanged.

The selector’s common field list stays in Org Common. Fieldset inclusion is additive: it adds registered fields to that list rather than removing common fields. Callers still use AccountsSelector.newInstance().selectById(accountIds).

Three Teams, One Selector

Each business package registers its own fieldset:

SalesAccountFields       -> SalesStage__c, ExpectedRevenue__c
MarketingAccountFields   -> Slogan__c, EngagementScore__c
ServiceAccountFields     -> SLATier__c, EscalationContact__c

Three teams, three fieldsets, one selector. Ownership stays with the packages that define the fieldsets. Once installed, their fields are included in shared selector queries for all callers; inclusion is not scoped to the team making the call.

What about a field two teams share? Put it in the Business Common package for exactly those two teams. Part 3 covered the rule: sales-marketing-common for fields only Sales and Marketing share, and sales-service-common for fields only Sales and Service share. Each fieldset binding then lives in the package whose queries need the fields, and the consumers depend only on the common packages they actually use.

Typing Tax: Each team now maintains an XML fieldset, a SelectorConfig_FieldSetInclusion__mdt record, and a deployment step. For three fields, that’s more ceremony than just adding the field. The payoff shows up when the third team arrives and nobody has to touch the shared class.

Testing a Selector With Injected Fields

Two kinds of tests are useful here.

A service unit test can mock the selector; Part 9 covers that setup. To check the actual fieldset wiring, use the real selector.

For integration tests, query through the real selector and confirm the injected field came back populated:

Account account = new Account(
    Name = 'TestCo',
    SalesStage__c = 'Prospecting'
);
insert account;

List<Account> results = AccountsSelector.newInstance().selectById(
    new Set<Id>{ account.Id }
);

System.assertEquals('Prospecting', results[0].SalesStage__c);

The integration test depends on the fieldset and its binding record being deployed. If the field is not selected, accessing it on the queried record raises an SObject exception, revealing the missing wiring.

Check Your Brew

1. Why is adding Account.SalesStage__c to getSObjectFieldList() a mistake?

  • A. It’s a syntax error in Apex
  • B. It disables field-level security
  • C. It makes Org Common depend on Sales-owned schema, so one team’s change can break every package
  • D. It slows down every query in the org
Reveal the answer

C. The shared selector would have to compile against a Sales field. That breaks the dependency direction, and it puts every team’s fields in one file.

2. What does SelectorConfig_FieldSetInclusion__mdt do?

  • A. Stores the SOQL query text for a selector
  • B. Registers a fieldset so the selector adds its fields to queries for that SObject
  • C. Defines a permission set for field access
  • D. Sets the page layout for Account
Reveal the answer

B. The record names the SObject and the fieldset. The selector loads every bound fieldset and adds those fields to the query.

3. Sales and Service both need SLATier__c, but Marketing doesn’t. Where does the field belong?

  • A. Org Common, for everyone
  • B. In a SalesAccountsSelector subclass
  • C. In the Marketing package
  • D. In a Business Common package for exactly Sales and Service, such as sales-service-common
Reveal the answer

D. A field shared by exactly two packages goes in the common package for those two. Marketing doesn’t depend on it, and no selector subclass is needed.

Next up: Part 6, Custom Brews, covers selector method injection, the way Marketing adds a query the shared selector doesn’t have. Fieldsets add what you see; methods decide what you get.