Selector Field Injection
Selector field injection lets downstream packages add fields to queries built by the one selector for an SObject. AccountsSelector remains in Org Common; Sales, Marketing, and Service do not create selector subclasses.
The Boundary Problem
Org Common cannot compile against a field owned by an upstream business package:
// 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
};
}
Creating SalesAccountsSelector would also be wrong: AT4DX applications keep one selector per SObject. Fieldset injection adds fields without either dependency violation.
Runtime Flow
flowchart LR
C[AccountsSelector.newInstance] --> A[AccountsSelector]
A --> B[Common fields from getSObjectFieldList]
A --> M[ApplicationSObjectSelector loads FieldSet bindings]
M --> SF[SalesAccountFields]
M --> MF[MarketingAccountFields]
B --> Q[Query factory]
SF --> Q
MF --> Q
Q --> SOQL[SOQL containing common + injected fields]
ApplicationSObjectSelector asks force-di for all Schema.FieldSet bindings for its SObject and adds them to the query factory. Field inclusion is additive.
Configure a Fieldset
Sales defines a fieldset in the package that owns or consumes the 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>
It registers that fieldset:
SelectorConfig_FieldSetInclusion__mdt
BindingSObject__c: Account
FieldsetName__c: SalesAccountFields
The source-tracked metadata record is:
<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Sales Account Fields</label>
<values>
<field>BindingSObject__c</field>
<value xsi:type="xsd:string">Account</value>
</values>
<values>
<field>FieldsetName__c</field>
<value xsi:type="xsd:string">SalesAccountFields</value>
</values>
</CustomMetadata>
The Org Common Selector
The selector has no Sales knowledge and is not designed for app-specific subclassing:
public interface IAccountsSelector extends IApplicationSObjectSelector {
List<Account> selectById(Set<Id> accountIds);
}
public with sharing class AccountsSelector
extends ApplicationSObjectSelector
implements IAccountsSelector {
public static IAccountsSelector newInstance() {
return (IAccountsSelector)
Application.Selector.newInstance(Account.SObjectType);
}
public Schema.SObjectType getSObjectType() {
return Account.SObjectType;
}
public override List<Schema.SObjectField> getSObjectFieldList() {
return new List<Schema.SObjectField>{
Account.Id,
Account.Name,
Account.Type,
Account.AnnualRevenue,
Account.OwnerId
};
}
private List<Schema.SObjectField> getAdditionalSObjectFieldList() {
return new List<Schema.SObjectField>();
}
public List<Account> selectById(Set<Id> accountIds) {
return (List<Account>) selectSObjectsById(accountIds);
}
}
A concrete AT4DX selector must override getSObjectFieldList() and implement getAdditionalSObjectFieldList(). Fieldset-injected fields are added by ApplicationSObjectSelector, not returned from the latter method.
Calling the Selector
Use the static factory wrapper and call the method inline:
List<Account> accounts = AccountsSelector.newInstance().selectById(accountIds);
Those Accounts include SalesStage__c, ExpectedRevenue__c, and SalesRep__c when the Sales fieldset binding is installed. AccountsSelector never names those fields.
If Sales needs a query that Org Common does not expose, add it with Selector Method Injection:
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectLargeAccountsSelectorMethod.class,
params
);
Field injection controls the selected fields; method injection controls app-specific query behavior.
Multiple Business Packages
Each package can register its own fieldset:
SalesAccountFields -> SalesStage__c, ExpectedRevenue__c
MarketingAccountFields -> Slogan__c, EngagementScore__c
ServiceAccountFields -> SLATier__c, EscalationContact__c
Schema shared by a particular pair belongs in the corresponding Business Common package. For example:
sales-marketing-commonowns fields shared only by Sales and Marketing;sales-service-commonowns fields shared only by Sales and Service.
The consuming apps depend only on the common packages they participate in. The fieldset binding can live with the package whose queries require those fields.
Testing
For unit tests, mock IAccountsSelector:
IAccountsSelector selector =
(IAccountsSelector) mocks.mock(IAccountsSelector.class);
mocks.startStubbing();
// Required: Application.Selector.setMock() asks the mock for its SObjectType.
mocks.when(selector.sObjectType()).thenReturn(Account.SObjectType);
mocks.when(selector.selectById(accountIds)).thenReturn(expectedAccounts);
mocks.stopStubbing();
Application.Selector.setMock(Account.SObjectType, selector);
An integration test can query through the real selector and assert a fieldset field was 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 fieldset and its SelectorConfig_FieldSetInclusion__mdt record must already be deployed for this integration test.