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 domain class we must setup the interface class.
/* IPortalUsers.cls */
public interface IPortalUsers extends IApplicationSObjectDomain {
Map<String, Boolean> validateUserSso(List<String> ssoId);
}
Domain Class
The domain class is made up of three sections.
- boilerplate code
- trigger handler methods (if needed)
- domain class methods
public inherited sharing class PortalUsers extends ApplicationSObjectDomain implements IPortalUsers {
// 1. Boilerplate code
public static IPortalUsers newInstance(List<PortalUser__c> records) {
return (IPortalUsers) Application.Domain.newInstance(records);
}
public static IPortalUsers newInstance(Set<Id> recordIds) {
return (IPortalUsers) Application.Domain.newInstance(recordIds);
}
public PortalUsers(List<PortalUser__c> records) {
super(records);
}
public class Constructor implements fflib_SObjectDomain.IConstructable {
public fflib_SObjectDomain construct(List<SObject> sObjectList) {
return new PortalUsers(sObjectList);
}
}
// 2. Trigger methods
public override void onBeforeInsert() {
this.validateUserSso(getRecords());
}
public override void onBeforeUpdate(Map<Id, SObject> existingRecords) {
this.validateUserSso(getChangedRecords(new Set<String>{ 'SSOId__c' }));
}
// 3. Domain methods
public Map<String, Boolean> validateUserSso() {
validateUserSso((List<PortalUser__c>)getRecords());
}
public Map<String, Boolean> validateUserSso(List<PortalUser__c> portalUsers) {
// run sso validation here
}
}
NOTE:
getRecords()
andgetChangedRecords()
are helper methods offered by thefflib_SObjectDomain
class, which is implemented in the Constructor.
Custom Metadata Records
Before we an use our flashly new Domain class, we need to setup the Application Factory Domain Binding. This can be accessed from within your Salesforce instance via:
Setup → Custom Metadata Types → ApplicationFactory_DomainBinding → New.
Key | Value |
---|---|
Label | PortalUsers |
ApplicationFactory_DomainBinding Name | PortalUsers |
Binding SObject (or Binding SObject Alternate) | PortalUser__c |
To | PortalUsers.Constructor |
Basic usage
List<PortalUser__c> portalUsers = new List<PortalUser__c>();
IPortalUsers iPortalUsers = (IPortalUsers) Application.Domain.newInstance(
portalUsers
);
// use the existing list of portalUsers that was passed to the domain class on initialisation
Map<String, Boolean> validatedUsersBySsoId = iPortalUsers.validateUserSso();
// use a new list of portalUsers
List<PortalUser__c> portalUsersNew = new List<PortalUser__c>();
Map<String, Boolean> validatedUsersBySsoId = iPortalUsers.validateUserSso(portalUsersNew);