GatherPhoneNumbersInScope

Overview

Step 2 of the pipeline (call path). Given a scope from BuildScopeForRecord, gathers all phone numbers associated with the in-scope records using the GatherPhoneNumbersFromRecords strategy. Returns a list of PhoneNumbersFromRecord — each entry links a normalised phone number back to the record it came from, enabling attribution in the final result.

Phone numbers can be narrowed before passing to step 3 — for example, to remove the user's own extension or a shared reception number. No widening is allowed: callers cannot inject phone numbers that were not produced by the strategy.

Request

Obtain the Request via BuildScopeForRecord.Response.toGatherPhoneNumbersRequest().

GatherPhoneNumbersInScope.IHandler handler =
    GatherPhoneNumbersInScope.construct(SharingRule.WithSharing);
GatherPhoneNumbersInScope.Response phoneResult =
    handler.execute(scopeResult.toGatherPhoneNumbersRequest());

The SharingRule passed to construct() is forwarded to the GatherPhoneNumbersFromRecords strategy — use WithSharing to respect the running user's CRM record visibility when gathering phone numbers from related records.

Response

Member Type Description
getPhoneNumbersFromRecords() List<PhoneNumbersFromRecord> Phone numbers gathered, each linked to the record it came from. Each PhoneNumbersFromRecord exposes recordRef (the source record) and toPhoneNumberSet() (the normalised numbers).
excluding(String phoneNumber) Response Removes a phone number from every record in the list. Narrowing only. Fluent.
toGetCallSegmentsRequest() GetCallSegments.Request Converts to a request for step 3. Collects all distinct phone numbers across all records.

Testing

MockGatherPhoneNumbersInScope is a package-provided test double. Call use() to install it for the current test transaction, then returning() with a list of PhoneNumbersFromRecord objects representing the phone numbers and CRM records that should be treated as in scope.

@isTest
static void myTest() {
    Id accountId = [SELECT Id FROM Account LIMIT 1].Id;

    PhoneNumbersFromRecord entry = new PhoneNumbersFromRecord(
        RecordRef.of(accountId),
        'Test Account',
        new List<String>{ '+441234567890' }
    );

    MockGatherPhoneNumbersInScope.use()
        .returning(new List<PhoneNumbersFromRecord>{ entry });

    // Your code that calls GatherPhoneNumbersInScope receives these phone numbers.
    MyService.run(someRecordId);
}
Method Description
use() Installs this instance as the handler for the current test transaction. Returns the instance for chaining.
returning(List<PhoneNumbersFromRecord> records) Configures the phone number entries returned by execute(). Passing null is equivalent to an empty list.