Skip to content

DML

Apex Classes: DML.cls and DML_Test.cls.

The lib main class for DML construction.

apex
new DML()
    .toInsert(new Account(Name = 'Beyond The Cloud'))
    .commitWork();

Methods

The following are methods for using DML:

INSERT

UPDATE

UPSERT

DELETE

UNDELETE

MERGE

PLATFORM EVENTS

FIELD-LEVEL SECURITY

SHARING MODE

CONFIGURATION

DEBUGGING

EXECUTION

INSERT

toInsert

Insert new records into the database.

Signature

apex
Commitable toInsert(SObject record);
Commitable toInsert(DML.Record record);
Commitable toInsert(Iterable<SObject> records);
Commitable toInsert(DML.Records records);

Example

sql
insert new Account(Name = 'My Account');
apex
new DML()
    .toInsert(new Account(Name = 'My Account'))
    .commitWork();
apex
Account account = new Account(Name = 'Parent Account');
Contact contact = new Contact(FirstName = 'John', LastName = 'Doe');

new DML()
    .toInsert(account)
    .toInsert(DML.Record(contact).withRelationship(Contact.AccountId, account))
    .commitWork();
apex
List<Account> accounts = new List<Account>{
    new Account(Name = 'Account 1'),
    new Account(Name = 'Account 2')
};
new DML()
    .toInsert(accounts)
    .commitWork();

UPDATE

toUpdate

Update existing records in the database.

Signature

apex
Commitable toUpdate(SObject record);
Commitable toUpdate(DML.Record record);
Commitable toUpdate(Iterable<SObject> records);
Commitable toUpdate(DML.Records records);

Example

sql
update account;
apex
Account account = [SELECT Id, Name FROM Account LIMIT 1];
account.Name = 'Updated Name';

new DML()
    .toUpdate(account)
    .commitWork();
apex
List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = null];

new DML()
    .toUpdate(DML.Records(contacts).with(Contact.AccountId, accountId))
    .commitWork();

UPSERT

toUpsert

Insert new records or update existing ones based on record ID.

Signature

apex
Commitable toUpsert(SObject record);
Commitable toUpsert(DML.Record record);
Commitable toUpsert(Iterable<SObject> records);
Commitable toUpsert(DML.Records records);

Example

sql
upsert accounts;
apex
List<Account> accounts = new List<Account>{
    new Account(Name = 'New Account'),           // Will be inserted
    new Account(Id = existingId, Name = 'Updated') // Will be updated
};

new DML()
    .toUpsert(accounts)
    .commitWork();

DELETE

toDelete

Delete records from the database.

Signature

apex
Commitable toDelete(Id recordId);
Commitable toDelete(SObject record);
Commitable toDelete(Iterable<Id> recordIds);
Commitable toDelete(List<SObject> records);

Example

sql
delete account;
apex
new DML()
    .toDelete(accountId)
    .commitWork();
apex
List<Account> accountsToDelete = [SELECT Id FROM Account WHERE Name LIKE 'Test%'];
new DML()
    .toDelete(accountsToDelete)
    .commitWork();

UNDELETE

toUndelete

Restore deleted records from the recycle bin.

Signature

apex
Commitable toUndelete(Id recordId);
Commitable toUndelete(SObject record);
Commitable toUndelete(Iterable<Id> recordIds);
Commitable toUndelete(List<SObject> records);

Example

sql
undelete account;
apex
new DML()
    .toUndelete(deletedAccountId)
    .commitWork();

MERGE

toMerge

Merge duplicate records (placeholder implementation).

Signature

apex
Commitable toMerge(SObject mergeToRecord, Id duplicateId);
Commitable toMerge(SObject mergeToRecord, SObject duplicateRecord);
Commitable toMerge(SObject mergeToRecord, List<SObject> duplicateRecords);
Commitable toMerge(SObject mergeToRecord, Iterable<Id> duplicateIds);

Example

sql
merge masterAccount duplicateAccount;
apex
new DML()
    .toMerge(masterAccount, duplicateAccount)
    .commitWork();

PLATFORM EVENTS

toPublish

Publish platform events.

Signature

apex
Commitable toPublish(SObject record);
Commitable toPublish(Iterable<SObject> records);

Example

sql
EventBus.publish(events);
apex
MyEvent__e event = new MyEvent__e(Message__c = 'Hello World');
new DML()
    .toPublish(event)
    .commitWork();

FIELD-LEVEL SECURITY

userMode

Execute DML operations respecting user permissions (default behavior).

Signature

apex
Commitable userMode();

Example

apex
new DML()
    .toInsert(new Case(Subject = 'User Mode Case'))
    .userMode()
    .commitWork();

systemMode

Execute DML operations bypassing user permissions.

Signature

apex
Commitable systemMode();

Example

apex
new DML()
    .toInsert(new Case(Subject = 'System Mode Case'))
    .systemMode()
    .commitWork();

stripNotCreatableFields

Remove fields that the current user cannot create.

Signature

apex
Commitable stripNotCreatableFields();

Example

apex
new DML()
    .toInsert(task)
    .systemMode()
    .stripNotCreatableFields()
    .commitWork();

stripNotUpdatableFields

Remove fields that the current user cannot update.

Signature

apex
Commitable stripNotUpdatableFields();

Example

apex
new DML()
    .toUpdate(accounts)
    .stripNotUpdatableFields()
    .commitWork();

SHARING MODE

withSharing

Execute DML operations enforcing sharing rules.

Signature

apex
Commitable withSharing();

Example

apex
new DML()
    .toInsert(accounts)
    .withSharing()
    .commitWork();

withoutSharing

Execute DML operations bypassing sharing rules.

Signature

apex
Commitable withoutSharing();

Example

apex
new DML()
    .toInsert(accounts)
    .withoutSharing()
    .commitWork();

CONFIGURATION

allowPartialSuccess

Allow some records to fail without rolling back successful operations.

Signature

apex
Commitable allowPartialSuccess();

Example

apex
List<Account> accounts = new List<Account>{
    new Account(Name = 'Valid Account'),
    new Account(), // Missing required Name field
    new Account(Name = 'Another Valid Account')
};

new DML()
    .toInsert(accounts)
    .allowPartialSuccess()
    .commitWork();

skipDuplicateRules

Skip duplicate rule evaluation during DML operations.

Signature

apex
Commitable skipDuplicateRules();

Example

apex
new DML()
    .toInsert(accounts)
    .skipDuplicateRules()
    .commitWork();

options

Provide custom Database.DmlOptions for DML operations.

Signature

apex
Commitable options(Database.DmlOptions options);

Example

apex
Database.DmlOptions options = new Database.DmlOptions();
options.optAllOrNone = false;
options.duplicateRuleHeader.allowSave = true;

new DML()
    .toInsert(accounts)
    .options(options)
    .commitWork();

commitHook

Register a hook to execute custom logic before and after commit.

Signature

apex
Commitable commitHook(DML.Hook callback);

Example

apex
public class MyHook implements DML.Hook {
    public void before() {
        System.debug('Before commit');
    }
    
    public void after() {
        System.debug('After commit');
    }
}

new DML()
    .toInsert(accounts)
    .commitHook(new MyHook())
    .commitWork();

DEBUGGING

preview

Preview DML operations without executing them.

Signature

apex
void preview();

Example

apex
new DML()
    .toInsert(account)
    .toUpdate(contact)
    .preview(); // Outputs debug information to logs

EXECUTION

commitWork

Execute all registered DML operations in a single transaction.

Signature

apex
void commitWork();

Example

apex
new DML()
    .toInsert(account)
    .toUpdate(contact)
    .toDelete(leadId)
    .commitWork();

Record Manipulation

Field Assignment

Assign field values to records using the with method.

apex
DML.Record(contact).with(Contact.Email, 'john@example.com')

Relationship Handling

Handle parent-child relationships in DML operations.

apex
DML.Record(contact).withRelationship(Contact.AccountId, account)
apex
DML.Record(contact).withRelationship(
    Contact.AccountId, 
    Account.External_Id__c, 
    'EXT123'
)

Factory Methods

DML.Record

Create a Record wrapper for single SObject manipulation.

Signature

apex
Record Record(Id recordId);
Record Record(SObject record);

Example

apex
DML.Record record = DML.Record(sobjectInstance);
DML.Record recordById = DML.Record(recordId);

DML.Records

Create a Records wrapper for multiple SObject manipulation.

Signature

apex
Records Records(List<SObject> records);
Records Records(Iterable<Id> recordIds);

Example

apex
DML.Records records = DML.Records(sobjectList);
DML.Records recordsByIds = DML.Records(recordIdSet);