registerNew, registerDirty, registerUpsert, and registerDelete methods in the Unit of Work

Unit of Work in Salesforce

Unit of Work (UoW) in Salesforce - fflib Apex Common

The Unit of Work (UoW) pattern, as implemented in the Salesforce fflib Apex Common library, helps manage database operations cleanly and efficiently. Below, we define the registerNew, registerDirty, registerUpsert, and registerDelete methods with examples using Account and Contact.

1. registerNew

Registers a new record to be inserted into the database. It tracks new SObjects that haven’t yet been persisted.


// Example: Registering a new Account and Contact
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork();

// Create a new Account
Account newAccount = new Account(Name = 'New Account');
// Register the new Account
uow.registerNew(newAccount);

// Create a new Contact linked to the Account
Contact newContact = new Contact(FirstName = 'John', LastName = 'Doe', AccountId = newAccount.Id);
// Register the new Contact
uow.registerNew(newContact);

// Commit the changes
uow.commitWork();

    

2. registerDirty

Registers an existing record that has been modified and needs to be updated in the database.


// Example: Registering an updated Account and Contact
Account existingAccount = [SELECT Id, Name FROM Account LIMIT 1];
existingAccount.Name = 'Updated Account Name';

// Register the Account as dirty
uow.registerDirty(existingAccount);

Contact existingContact = [SELECT Id, FirstName, LastName FROM Contact LIMIT 1];
existingContact.LastName = 'Updated Last Name';

// Register the Contact as dirty
uow.registerDirty(existingContact);

// Commit the changes
uow.commitWork();

    

3. registerUpsert

Registers a record for an upsert operation. This means it will either insert the record if it doesn’t exist or update it if it does.


// Example: Registering an upsert operation for Account and Contact
Account upsertAccount = new Account(Id = '0015g00000XXXXX', Name = 'Upserted Account');
// Register the Account for upsert
uow.registerUpsert(upsertAccount);

Contact upsertContact = new Contact(Id = '0035g00000YYYYY', FirstName = 'Upserted John', LastName = 'Doe');
// Register the Contact for upsert
uow.registerUpsert(upsertContact);

// Commit the changes
uow.commitWork();

    

4. registerDelete

Registers a record to be deleted from the database.


// Example: Registering a deletion for Account and Contact
Account accountToDelete = [SELECT Id FROM Account LIMIT 1];
// Register the Account for deletion
uow.registerDelete(accountToDelete);

Contact contactToDelete = [SELECT Id FROM Contact LIMIT 1];
// Register the Contact for deletion
uow.registerDelete(contactToDelete);

// Commit the changes
uow.commitWork();

    

Conclusion

The Unit of Work (UoW) pattern allows you to manage multiple DML operations in a clean and transactional manner. Using methods like registerNew, registerDirty, registerUpsert, and registerDelete, you can ensure that all changes are tracked and committed efficiently in bulk.

For more details, refer to the fflib_SObjectUnitOfWork documentation.

registerNew, registerDirty, registerUpsert, and registerDelete methods in the Unit of Work registerNew, registerDirty, registerUpsert, and registerDelete methods in the Unit of Work Reviewed by dasfrogpractice on 09:27 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe