Smart DML Management with Unit of Work Pattern in Salesforce

Ben Butler
2 min readSep 24, 2021

Financial Force has provided us a convenient unit of work (UOW) pattern. Martin Fowler originally described it as a way manage the nuances of making database statements.

We can consolidate DML we want to do into a single command and save actually performing a single DML operation until after all of the business logic is executed.

It works by piling all DML we want to make into the UOW pattern by

  • “registering” the changes to store the future DML changes
  • “committing” the changes to actually perform the DML

The Register Methods

registerNew

  • These records will be inserted on commit
public void registerNew(List<SObject> records)public void registerNew(SObject record, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)

registerDirty

  • These records will be updated on commit
public void registerDirty(SObject record)public void registerDirty(List<SObject> records, List<SObjectField> dirtyFields)public void registerDirty(SObject record, List<SObjectField> dirtyFields)public void registerDirty(SObject record, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)public void registerDirty(List<SObject> records)

registerUpsert

  • These records will be upserted on commit
public void registerUpsert(SObject record)public void registerUpsert(List<SObject> records)

registerDeleted

  • These records will be put into the recycle bin on commit
public void registerDeleted(SObject record)public void registerDeleted(List<SObject> records)

registerPermanentlyDeleted

  • These records will be both deleted and removed from the recycle bin on commit
public void registerPermanentlyDeleted(List<SObject> records)public void registerPermanentlyDeleted(SObject record)

registerEmptyRecycleBin

  • Registers an already-deleted record to be removed from the recycling bin
public void registerEmptyRecycleBin(SObject record)public void registerEmptyRecycleBin(List<SObject> records)

registerPublishBeforeTransaction

  • Register platform events to be published on commit before the DML
public void registerPublishBeforeTransaction(SObject record)public void registerPublishBeforeTransaction(List<SObject> records)

registerPublishAfterSuccessTransaction

  • Register platform events to be published on commit after the DML
public void registerPublishAfterSuccessTransaction(SObject record)public void registerPublishAfterSuccessTransaction(List<SObject> records)

registerPublishAfterFailureTransaction

  • Register platform events to be published on commit after the DML fails
public void registerPublishAfterFailureTransaction(SObject record)public void registerPublishAfterFailureTransaction(SObject record)

registerRelationship

  • Relates a records lookup relationship that are currently only related by External Id on commit
public void registerRelationship(SObject record, Schema.SObjectField relatedToField, SObject relatedTo)public void registerRelationship( Messaging.SingleEmailMessage email, SObject relatedTo )public void registerRelationship(SObject record, Schema.SObjectField relatedToField, Schema.SObjectField externalIdField, Object externalId)

--

--