-
Notifications
You must be signed in to change notification settings - Fork 24
06) The fflib_SObjectUnitOfWork Class
- registerNew() - Registers a single record or list of records as new records that need to be created
- registerRelationship(SObject, SObjectField, SObject) - Registers a relationship between one record and another
- registerDirty() - Registers a single record or list of records to update
- registerDeleted() - Registers a single record or list of records to be deleted
- registerEmail(EmailMessage) - Registers an email message to be sent
- registerWork(IDoWork) - Registers a callback method to be called after your work has been committed to the database.
- commitWork() - Commits your unit of work (records registered) to the database. This should always be called last.
There are many other useful methods in the fflib_SObjectUnitOfWork class, but the above methods are the major ones you'll leverage most frequently.
The following code example shows you how to setup a callback method for your units of work, should you need them.
public inherited sharing class HelpDeskAppPostCommitLogic implements fflib_SObjectUnitOfWork.IDoWork{
List<Task> taskList;
public HelpDeskAppPostCommitLogic(List<Task> taskList){
this.taskList = taskList;
}
public void doWork(){
//write callback code here
}
}
The code below shows you how to actually make sure your unit of work calls your callback method.
fflib_ISObjectUnitOfWork uow = Helpdesk_Application.helpDeskUOW.newInstance();
//code to create some tasks
uow.registerNew(newTasks);
uow.registerWork(new HelpDeskAppPostCommitLogic(newTasks));
uow.commitWork();
-
Records within the same object that have lookups to each other are currently not supported. For example, if the Account object has a Lookup to itself, that relationship cannot be registered.
-
You cannot do an all or none false database transactions.
Database.insert(acctList, false);
- To send emails with the Apex Commons UOW you must utilize the special registerEmail method.
To do these things in your own way you would need to make a new class that implements the fflib_SObjectUnitOfWork's IDML interface which we'll cover below
If your unit of work needs a custom implementation for inserting, updating, deleting, etc that is not supported by the SimpleDML inner class then you are gonna want to create a new class that implements the fflib_SObjectUnitOfWork.IDML
interface. After you create that class if you were using the Application factory you would instantiate your unit of work like so Application.uow.newInstance(new customIDMLClass());
otherwise you would initialize it using public static fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new List<SObjectType>{Case.SObjectType}, new customIDMLClass());
Example of an IDML Class
public customIDMLClass implements fflib_SObjectUnitOfWork.IDML{
}