App definitions and loaders normally filter records with SOQL WHERE clauses (selectionClause in an app definition, filterRecordsBy in a loader). SOQL has hard limits though. Most notably a semi-join can only cross one relationship level, so this clause is rejected by Salesforce:
b3s__Work_Job__r.b3s__Account__c IN (SELECT b3s__Account__c FROM b3s__Group_Assignment__c WHERE b3s__Group__c = :contextId)
Error: The left operand
b3s__Work_Job__r.b3s__Account__ccannot have more than one level of relationships.
An Apex filter solves this. Instead of a clause, the configuration names an Apex class. The server runs that class, the class returns the set of record Ids that pass the filter, and the query is restricted to those Ids (Id IN :ids). Anything Apex can compute can now drive what an app loads.
Add an apexFilter key to the JSON configuration, naming the class (namespace qualified where one exists).
In an app definition schedulable (b3s__Scheduling_Context_Provider__mdt or b3s__Component_Configuration__mdt):
"shift": {
"objectType": "b3s__Shift__c",
"apexFilter": "b3s.GroupShiftLoader"
}
In a loader (an entry in the definition record's b3s__Loaders__c, or a legacy b3s__Scheduling_Loader__mdt / b3s__Component_Loader__mdt b3s__JSON__c):
{
"name": "groupPatterns",
"objectName": "b3s__Working_Pattern__c",
"fieldToLoad": ["Name", "b3s__Start__c", "b3s__End__c"],
"apexFilter": "b3s.GroupWorkingPatternLoader"
}
Rules:
apexFilter can stand alone (no selectionClause or filterRecordsBy needed), in which case the plugin's Ids are the only filter.loadRecords, matchApexFilter pairs with matchSelectionClause the same way apexFilter pairs with selectionClause, so marketplace and self-search queries stay independently configurable."b3s.GroupShiftLoader" for a packaged plugin, or just "MyOrgFilter" for a class built in your own org. Namespace qualified names still resolve in orgs without that namespace (sandboxes and scratch orgs), so packaged configuration stays portable.These plugins are included as sample Apex classes with the package, deployed into your org alongside it (rather than locked inside the managed package) so you can read and adapt them. In a subscriber org they are local (un-namespaced) classes, and configuration may still reference them as b3s.<Name> because name resolution falls back to the local namespace.
| Plugin | Object | What it loads |
|---|---|---|
b3s.GroupShiftLoader |
b3s__Shift__c |
Shifts whose job's account or site's account has a Group Assignment for the contextual group, starting within the loaded period. |
b3s.GroupWorkingPatternLoader |
b3s__Working_Pattern__c |
Working Patterns whose job's account or site's account has a Group Assignment for the contextual group and whose start and end dates overlap the loaded period (undated patterns always load). |
b3s.GroupLocationLoader |
b3s__Site_Location__c |
Site Locations whose site's account has a Group Assignment for the contextual group. No date filter, the group is the only context used. |
GroupRateGroupLoader |
b3s__Rate_Group__c |
Rate Groups whose job's account has a Group Assignment for the contextual group. No date filter, the group is the only context used. |
All three are referenced by the Group scheduling context provider and loaders, where the context record (:contextId) is the sharing group being scheduled.
Implement the packaged global interface FilterPlugin:
global interface FilterPlugin {
Set<Id> execute(Map<String, Object> context);
}
Return the Ids of the records that should load. Returning an empty set (or null) loads nothing. Your class needs a public no-arg constructor (the default one is fine), because it is instantiated with Type.newInstance().
Minimal org level example:
public with sharing class MyOrgFilter implements b3s.FilterPlugin {
public Set<Id> execute(Map<String, Object> context) {
Id contextId = (Id) context.get('contextId');
Datetime windowStart = (Datetime) context.get('contextStart') ?? Datetime.newInstanceGmt(1970, 1, 1);
// ... any Apex you need ...
return new Map<Id, b3s__Shift__c>([
SELECT Id FROM b3s__Shift__c
WHERE /* your filter */ b3s__Absolute_Start_Time__c >= :windowStart
]).keySet();
}
}
Reference it from configuration as "apexFilter": "MyOrgFilter".
| Key | Type | Value |
|---|---|---|
contextId |
Id (nullable) |
The contextual record the app runs against, for example the sharing group in group scheduling. |
contactId |
String (nullable) |
The acting contact. For portal sessions this is always the signed-in contact. |
contextStart |
Datetime (nullable) |
Start of the period the app is loading, parsed from the client's fetch window. |
contextEnd |
Datetime (nullable) |
End of the loaded period. |
contextDate |
String (nullable) |
The single contextual date some component apps send (YYYY-MM-DD). |
accountIds |
Set<Id> |
The accounts the acting contact acts for, the same set the :accountIds token resolves to (own Account without an access profile, Account Membership accounts with one), narrowed to the app's account selection. Never null; empty when there are none. See VMS access profiles and account memberships. The packaged vendor pool filter uses it to build the client's vendor pool across all of the contact's accounts. |
Nullable means nullable, so guard every key. The window keys are absent when the caller does not send a fetch window (for example some loader invocations), and contextId is absent when the app runs without a context record.
Id IN :apexFilterIds to the query's WHERE clause, with the Ids travelling as a bind variable. There is no SOQL statement length limit on the number of Ids, and the standard Id cursor pagination (hasMore / nextLastId) works unchanged on top of it.execute cheap, because it counts against the synchronous limits of the request that triggered it (SOQL rows, CPU, heap). The 50k row query limit inside your own plugin's SOQL is usually the ceiling, so filter as hard as you can inside the plugin query itself.The configured name resolves in this order:
ns.Class resolves as Type.forName('ns', 'Class').ns.Class fallbacks: the same string as a local Outer.Inner, then the bare class name in the current namespace, then in the org's default namespace. So "b3s.GroupShiftLoader" resolves whether the class is packaged, deployed into a namespaced scratch org, or deployed as unmanaged metadata into a subscriber org.Class: current namespace first, then the org's default namespace, so subscriber orgs can ship plugins without any namespace.If nothing resolves, or the class does not implement FilterPlugin, the request fails with Apex filter [...] not found or does not implement FilterPlugin.
apexFilter to a definition with the same care as writing the clause it replaces.with sharing is right for apps used by signed-in Salesforce users. Both packaged plugins do this.without sharing, or it returns zero Ids, because portal contacts own no records. Scope its query with the contactId and accountIds context keys.new MyPlugin().execute(context) with a hand built context map, and assert on the returned Id set. The sample plugins ship with their own unit tests you can use as a template.