DML Lib is a lightweight Apex library with a fluent API for DML operations: insert, update, upsert, delete, undelete, merge and publish platform events.
DML Lib is part of Apex Fluently, a suite of production-ready Salesforce libraries by Beyond the Cloud.
- Fluent API - register records and commit them in one readable chain
- Relationship handling - parent IDs are set on child records automatically, in any registration order
- Fewer DML statements - records are grouped into one statement per object type
- Security control - user mode by default, system mode with or without sharing when you need it
- Transactions - roll back everything on failure with
commitTransaction(), validate first withdryRun() - Detailed results - successes, failures and errors for every operation and record
- Mocking - test DML logic without touching the database
Insert related records
Account account = new Account(Name = 'Acme Corp');
Contact newContact = new Contact(LastName = 'Smith');
new DML()
.toInsert(account)
.toInsert(DML.Record(newContact).withRelationship(Contact.AccountId, account))
.commitWork();
// newContact.AccountId is set to account.IdCombine operations in one unit of work
new DML()
.toInsert(newAccounts)
.toUpdate(existingContacts)
.toDelete(obsoleteTasks)
.commitWork();Run in system mode
new DML()
.toUpdate(accounts)
.systemMode()
.withoutSharing()
.commitWork();Mock DML in unit tests
public class AccountService {
public void createAccount() {
new DML()
.toInsert(new Account(Name = 'Acme'))
.identifier('AccountService.createAccount')
.commitWork();
}
}@IsTest
static void shouldCreateAccount() {
DML.mock('AccountService.createAccount').allInserts();
Test.startTest();
new AccountService().createAccount();
Test.stopTest();
DML.Result result = DML.retrieveResultFor('AccountService.createAccount');
Assert.areEqual(1, result.insertsOf(Account.SObjectType).successes().size());
}Install DML Lib as an unlocked package (btcdev namespace), as an unmanaged package, or deploy the source directly. Package links are on the Installation page.
Everything else (all DML operations, mocking, results, configuration and advanced features) is covered in the documentation.
- For proper license management each repository should contain a LICENSE file similar to this one.
- Each original class should contain a copyright mark: © Copyright 2026, Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)