Basic Example

Basic Example

Our example is using a custom object called PortalUser__c, but this example can be used with any object.

Interface Class

Before we can build the main selector class we must setup the interface class.

/* IPortalUserSelector.cls */
public interface IPortalUserSelector extends IApplicationSObjectSelector {
  List<PortalUser__c> selectById(Set<Id> idSet);
  PortalUser__c selectById(Id idSet);
  List<PortalUser__c> selectAll();

Main Selector Class

The main selector class is made up of three sections:

  1. sObjectField list
  2. boilerplate code
  3. selector methods

NOTE: that our sharing is marked as inherited sharing. Sharing should be handled at the Controller Layer.

/* PortalUserSelector.cls */
public inherited sharing class PortalUserSelector extends ApplicationSObjectSelector implements IPortalUserSelector {
  /* 1. sObjectField list */
  private List<String> additionalSObjectFieldList = new List<String>{
  /* 2. Boilerplate code */
  public static PortalUserSelector newInstance() {
    return (PortalUserSelector) Application.Selector.newInstance(
  public Schema.SObjectType getSObjectType() {
    return PortalUser__c.SObjectType;
  public fflib_QueryFactory getQueryFactory() {
    return new fflib_QueryFactory(getSObjectType());
  public override List<Schema.SObjectField> getSObjectFieldList() {
    return new List<Schema.SObjectField>();
  public List<String> getSObjectFieldListWithRelatedFields() {
    return this.additionalSObjectFieldList;
  public void setSObjectFieldListWithRelatedFields(String[] values) {
    this.additionalSObjectFieldList = values;
  private List<Schema.SObjectField> getAdditionalSObjectFieldList() {
    return new List<Schema.SObjectField>{};
  /* 3. Selector Methods */
  public List<PortalUser__c> selectById(Set<Id> idSet) {
    if (idSet.isEmpty()) {
      return new List<PortalUser__c>();
    fflib_QueryFactory qf = getQueryFactory();
    qf.setCondition('Id IN :idSet');
    return Database.query(qf.toSOQL());
  public PortalUser__c selectById(Id recordId) {
    List<PortalUser__c> recordList = selectById(new Set<Id>{ recordId });
    return recordList.isEmpty() ? null : recordList[0];
  public List<PortalUser__c> selectAll() {
    fflib_QueryFactory qf = getQueryFactory();
    return Database.query(qf.toSOQL());

Custom Metadata Records

Before we an use our flashly new Selector class, we need to setup the Application Factory Selector Binding. This can be accessed from within your Salesforce instance via:

Setup → Custom Metadata Types → ApplicationFactory_SelectorBinding → New.

ApplicationFactory_SelectorBinding NamePortalUserSObjectBinding
Binding SObject (or Binding SObject Alternate)PortalUser__c

Basic Usage

Here we are creating a new instance of our selector method and then calling the selectById method.

/* PortalUserService.cls */
IPortalUserSelector portalUserSelector = (IPortalUserSelector)Application.Selector.newInstance(
List<PortalUser__c> portalUsers = portalUserSelector.selectById(
  new Set<Id>{'SF_RECORD_ID'}