3

I am getting a JSON response with multiple account records which I have to upsert in Account object. I am trying to implement this requirement using UOW provided by the fflib library.

Is there a way to allow partial record processing using UOW if errors are encountered something like below?

Database.SaveResult[] srList = Database.insert(acctList, false);

I need to send a response such that the sender will come to know which records were processed successfully.

2 Answers 2

2

As far as I'm aware, no. FFLib's UOW is designed to be an all-or-none affair. If all records are processed successfully, it returns success. If any records fail anywhere in the UOW, the entire thing is rolled back. You could add this functionality, but it could become a messy affair; you'd need to figure out how to roll back parent objects, potentially recursively.

2

The only way to do partial success Database updates in fflib_SObjectUnitOfWork is to delegate to an IDoWork implementation.

Class that will do the partial success Database operations

public class MyAccountUpdater implements fflib_SObjectUnitOfWork.IDoWork {
    SomeType xxx; 

    MyAccountUpdater(SomeType val) {this.xxx = val;}

    public void doWork() {
        Database.UpsertResult[] results = Database.upsert(...);
        for (Database.UpsertResult resul: results) {..}
    }
}

Main code path

In your main body of logic, you set up the unit of work, registerXXX, and especially register the IDoWorker(s)

fflib_ISobjectUnitOfWork uow = Application.UnitOfWork.newInstance(); 
uow.registerWork(new MyAccountUpdater(..));
..other stuff including registerNew, registerDirty, registerDeleted
uow.commitWork();

Once the registerNew, registerDirty, and registerDeleted objects are DML'd by fflib, the UnitOfWork will call all of the registered IDoWorkers.

If your unit of work is solely the partial upsert of Accounts, then all this folderol of the fflib unit of work is distracting although at least you can do some unit test mocking of the IDoWorker.

2
  • Nice answer. Thanks. There's a good post about using doWork in one of Andy in the Cloud's posts: andyinthecloud.com/2014/07/17/…
    – lemming
    Sep 30, 2021 at 13:47
  • 1
    @lemming since that post was made UnitOfWork now supports more operations including sending email and publishing platform events
    – cropredy
    Sep 30, 2021 at 14:16

You must log in to answer this question.

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