Apex filters

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__c cannot 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.

How to use one

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:

Bundled plugins

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.

The contract (for developers)

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".

The context map

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.

How the Ids are applied

Name resolution

The configured name resolves in this order:

  1. ns.Class resolves as Type.forName('ns', 'Class').
  2. 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.
  3. Bare 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.

Access and sharing

Testing