Accelerators & Utilities
Our ambition is to reduce code repetitions and ease the life of developers, therefore a proper set of utilities and accelerating methods and defaults couldn't be missed.
D'fflib comes with a set of ready-to-use utilities and accelerators to speed up your Apex development. Below is the list of method signatures with a brief description of their purpose, per base-class.
dfflib_Application
Simplified & standardised mocks
Eliminating multiple lines of (boilerplate) code, making it easier to remember and avoiding failures due to accidentally forgotten lines. A single line per mock, identical for all layers, takes away the concerns of constructing, defining and setting a stubbed/mocked class. Improving readability and helping new-joiners to more easily understand and apply the code.
fflib_ApexMocks mocks = new fflib_ApexMocks();
dfflib_ISObjectUnitOfWork uowMock = dfflib.unitOfWork.defineMock( mocks );
DOM_Accounts m_domAccounts = ( DOM_Accounts ) dfflib.domain.defineMock( mocks, DOM_Accounts.class, Account.SObjectType );
SEL_Accounts m_selAccounts = ( SEL_Accounts ) dfflib.selector.defineMock( mocks, SEL_Accounts.class, Account.SObjectType );
SRV_Response m_srvResponse = ( SRV_Response ) dfflib.service.defineMock( mocks, SRV_Response.class );
SRV_Response m_srvIResponse = ( SRV_Response ) dfflib.service.defineMock( mocks, SRV_Response.class, SRV_Response.IService.class );fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_ISObjectUnitOfWork uowMock = ( fflib_ISObjectUnitOfWork ) mocks.mock( fflib_ISObjectUnitOfWork.class );
DOM_Accounts m_domAccounts = ( DOM_Accounts ) mocks.mock( DOM_Accounts.class );
SEL_Accounts m_selAccounts = ( SEL_Accounts ) mocks.mock( SEL_Accounts.class );
SRV_Response m_srvResponse = ( SRV_Response ) mocks.mock( SRV_Response.class );
SRV_Response m_srvIResponse = ( SRV_Response ) mocks.mock( SRV_Response.IService.class );
mocks.startStubbing();
mocks.when( m_domAccounts.sObjectType() ).thenReturn( Account.SObjectType );
mocks.when( m_selAccounts.sObjectType() ).thenReturn( Account.SObjectType );
mocks.stopStubbing();
Application.domain.setMock( m_domAccounts );
Application.selector.setMock( m_selAccounts );
Application.service.setMock( SRV_Response.class, m_srvResponse );
Application.service.setMock( SRV_Response.IService.class, m_srvIResponse );
Application.unitOfWork.setMock( uowMock );dfflib_Utils
Given that all proper methods should validate their inputs, and provide a user-friendly error,
dfflib_Utils provides a set of utility methods and Exception-definitions to facilitate input validation and string population:
Base Exceptions
dfflib_Utils provides three ready-to-use Exceptions:
dfflib_Utils.InvalidImplementationException
dfflib_Utils.InvalidInputValueException
dfflib_Utils.MissingDataExceptionInput verifications
Methods which throw a dfflib_Utils.InvalidInputValueException when the verification fails.
dfflib_Utils.verifyNotBlank( String param, String paramName );dfflib_Utils.verifyNotEmpty( List<Object> objList, String paramName );dfflib_Utils.verifyNotNull( Object param, String paramName );
No verifyNotEmpty( Set<Object> )
Due to the invariant implementation of Sets in Apex Set<Account> or Set<Id> are never an instance of Set<Object>.
As a workaround, one could initiate their own input-specific verify-method, or dfflib_Utils.verifyNotEmpty( new List<Object>( mySet ), 'paramName' );.
Though, be conscious of the additional conversion overhead.
String construction
dfflib_Utils.message( String message, String substitutionString );
Shortcut to properly inject fields into a String without too many String manipulations. ReturningString.format( message, new List<String>{ substitutionString } );.
fflib_SObjects
D'fflib brings a full set of Collection utilities, which - after alignment with AEP core team - have been backported
to fflib-apex-common, to prevent the need for introducing an extension-version for fflib_SObjects.
These methods are available in any Domain method, since dfflib_SObjectDomain extends fflib_SObjects.
Methods with a * are public and can also be called outside a Domain, via new fflib_SObjects( records ). All others are protected: expose them via a Domain business method.
List utilities
(Semi-)alphabetical list of utility methods which consume the records (List<SObject>) variable via the getRecords() method:
Update field values
void clearField( Schema.SObjectField field )*
Clears (nullifies) a single field on all provided records.void clearFields( Set<Schema.SObjectField> fields )*
Clears (nullifies) multiple fields on all provided records.void setFieldValue( Schema.SObjectField field, Object value )
Sets the same value on the given field for all provided records.void setFieldValueByMap( Schema.SObjectField fieldToCheck, Schema.SObjectField fieldToUpdate, Map<Object, Object> values )
Sets the field value (to update) with the Map-value, sharing the same key as the value infieldToCheck. Records without matching key are untouched.
Collect values for a specific field (e.g. all Ids, or Statuses)
Set<Id> getRecordIds()*
Returns the record Ids of all provided records.Set<Object> getFieldValues( Schema.SObjectField field )
Collects and returns a Set of values for the given field across all records.Set<Id> getIdFieldValues( Schema.SObjectField field [, Boolean includeNulls] )
Used for fetching Ids from given Id field. By defaultnullis added to the result set.Set<Id> getParentIdFieldValues( String fieldWithDotRelation )*
Plucks Ids via a dotted relationship path of any depth (e.g.'Owner.ContactId'or'Parent.Owner.ManagerId'), excluding null values. Records with an unpopulated intermediate relationship are skipped.Set<String> getStringFieldValues( Schema.SObjectField field [, Boolean includeNulls] )
Used for fetching String values from given field. By defaultnullis added to the result set.
Filter List by field values
List<SObject> getRecordsByFieldValue( Schema.SObjectField field, Object value )
Returns records where the given field equals the provided value.List<SObject> getRecordsByFieldValues( Schema.SObjectField field, Set<Object> values )
Returns records where the given field matches any value in the provided set.List<SObject> getRecordsWithAllBlankFieldValues( Set<Schema.SObjectField> fields )
Returns records where all given fields are blank (null or empty string).List<SObject> getRecordsWithAllNotBlankFieldValues( Set<Schema.SObjectField> fields )
Returns records where all given fields are populated.List<SObject> getRecordsWithBlankFieldValues( Schema.SObjectField field )
Convenience overload to filter records where a single field is blank (null or empty string).List<SObject> getRecordsWithBlankFieldValues( Set<Schema.SObjectField> fields )
Returns records where at least one of the given fields is blank.List<SObject> getRecordsWithNotBlankFieldValues( Schema.SObjectField field )
Convenience overload to filter records where a single field is populated.List<SObject> getRecordsWithNotBlankFieldValues( Set<Schema.SObjectField> fields )
Returns records where at least one of the given fields is populated.
Group List by field values to a Map
Map<Object, List<SObject>> groupRecordsByFieldValue( Schema.SObjectField identifierField )*
Method to easily construct a Map from a List of SObjects, with key set by the value of the identifier field. Throws afflib_Objects.InvalidValueExceptionwhen a record has no value for the given field.
fflib_SObjectDescribe
Similarly to the utility methods in fflib_SObjects, D'fflib provides static picklist-utility-methods
which can now also
be found in fflib-apex-common.
Picklist related utilities
When in need to validate or process picklist values or users' input, you can leverage the out-of-the-box, static, centralised and optimised methods related to SObject definitions.
fflib_SObjectDescribe.getPicklistLabels( Schema.SObjectField selectedField )
Returns the labels of a picklist field as aList<String>, unmodified. UsegetPicklistValues()orisValidPicklistValue()for API-value comparisons.fflib_SObjectDescribe.getPicklistValues( Schema.SObjectField selectedField )
Returns theList<Schema.PicklistEntry>(label and value) of a picklist field; returns an empty list for non-picklist fields.fflib_SObjectDescribe.isValidPicklistValue( Schema.SObjectField sObjField, String value )
Returns TRUE when the value is a valid (case-insensitive) picklist API value for the provided field; FALSE for a blank or non-matching value.