0

The commitWork() method in the class fflib_SObjectUnitOfWork has a predefined order of execution that always sends the emails at last, as it can be seen as follows:

private void doCommitWork()
{
        onCommitWorkStarting();
        onPublishBeforeEventsStarting();
        publishBeforeEventsStarting();
        onPublishBeforeEventsFinished();

        onDMLStarting();
        insertDmlByType();
        updateDmlByType();
        deleteDmlByType();
        emptyRecycleBinByType();
        resolveEmailRelationships();
        onDMLFinished();

        onDoWorkStarting();
        doWork();
        onDoWorkFinished();
        onCommitWorkFinishing();
}  

private void doWork()
{
        m_workList.add(m_emailWork);
        for (IDoWork work : m_workList)
        {
            work.doWork();
        }
}

Source

I would like to send the emails first and then execute the update DML. Is this possible? I would like to send an email with the state of the objects before updating the objects but with the fflib unit of work I am forced to always send the emails at last.

1 Answer 1

1

There is an open issue in the fflib/apex-common repo to address this

When I had this issue, I implemented everything in my IDoWork worker so I could get the order I wanted.

2
  • Hi, that is great. One thing, I see that the method fflib_SObjectUnitOfWork.registerWork is used to register pieces of work but I do not see any method to retrieve the pieces of work. How Do you retrieve the pieces of work registered using fflib_SObjectUnitOfWork.registerWork? Commented Jul 18, 2020 at 8:24
  • 1
    You can't. And in theory, you shouldn't need to. The objects presented to registerXXX are your application's objects so the application should keep track of which ones are new v dirty v deleted.
    – cropredy
    Commented Jul 18, 2020 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .