---
url: https://apexfluently.beyondthecloud.dev/libraries/dml-lib.md
description: >-
  Manage DML operations with mocking, automatic relationship handling, and
  comprehensive results. Unit of Work pattern alternative.
---

# DML Lib

The DML Lib provides functional constructs for DMLs in Apex.

::: info Documentation
You can find the full documentation at [dml.beyondthecloud.dev](https://dml.beyondthecloud.dev).
:::

## What Does It Solve?

* **You Have Uncommitted Work Pending** - Callouts are not allowed when there is an uncommitted transaction pending.
* **Too Many DML Statements** - Too many DML operations issued in a single Apex transaction.
* **Long-Running Unit Tests** - Unit tests take a long time to execute due to complex data creation.
* **Code Performance** - Inefficient code due to DML operations scattered across the codebase.
* **Lack of Transaction Control** - No centralized way to manage and commit database operations.

## Why DML Lib?

* **Mock DMLs in Apex Tests** - Built-in mocking support lets you test your code without database operations, making tests faster and more isolated.
* **Comprehensive Results** - Get detailed operation results including success/failure status, record IDs, and error information for each DML operation.
* **Security Controls** - Full support for User/System mode and With/Without Sharing, plus field-level security stripping.
* **Relationship Handling** - Automatically resolve parent-child relationships across inserts. No need to manually assign IDs after parent inserts.
* **Immediate Operations** - Execute single DML operations immediately and get results without needing to call commitWork().

## Quick Start

```apex
Account account = new Account(Name = 'Acme Corp');
Contact contact = new Contact(LastName = 'Smith');

new DML()
    .toInsert(account)
    .toInsert(DML.Record(contact).withRelationship(Contact.AccountId, account))
    .commitWork();

// contact.AccountId is automatically set to account.Id
```

## Documentation

* [GitHub Repository](https://github.com/beyond-the-cloud-dev/dml-lib)
* [Documentation](https://dml.beyondthecloud.dev/)

## Installation

* [Install via Unlocked Package](https://dml.beyondthecloud.dev/installation.html#install-via-unlocked-package)
* [Install via Unmanaged Package](https://dml.beyondthecloud.dev/installation.html#install-via-unmanaged-package)
* [Copy and Deploy](https://dml.beyondthecloud.dev/installation.html#copy-and-deploy)
