DML
Apex Classes: DML.cls and DML_Test.cls.
The lib main class for DML construction.
new DML()
.toInsert(new Account(Name = 'Beyond The Cloud'))
.commitWork();Methods
The following are methods for using DML:
toInsert(SObject record)toInsert(DML.Record record)toInsert(Iterable<SObject> records)toInsert(DML.Records records)
toUpdate(SObject record)toUpdate(DML.Record record)toUpdate(Iterable<SObject> records)toUpdate(DML.Records records)
toUpsert(SObject record)toUpsert(DML.Record record)toUpsert(Iterable<SObject> records)toUpsert(DML.Records records)
toDelete(Id recordId)toDelete(SObject record)toDelete(Iterable<Id> recordIds)toDelete(List<SObject> records)
toUndelete(Id recordId)toUndelete(SObject record)toUndelete(Iterable<Id> recordIds)toUndelete(List<SObject> records)
toMerge(SObject mergeToRecord, Id duplicateId)toMerge(SObject mergeToRecord, SObject duplicateRecord)toMerge(SObject mergeToRecord, List<SObject> duplicateRecords)toMerge(SObject mergeToRecord, Iterable<Id> duplicateIds)
allowPartialSuccess()skipDuplicateRules()options(Database.DmlOptions options)commitHook(DML.Hook callback)
INSERT
toInsert
Insert new records into the database.
Signature
Commitable toInsert(SObject record);
Commitable toInsert(DML.Record record);
Commitable toInsert(Iterable<SObject> records);
Commitable toInsert(DML.Records records);Example
insert new Account(Name = 'My Account');new DML()
.toInsert(new Account(Name = 'My Account'))
.commitWork();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();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
Commitable toUpdate(SObject record);
Commitable toUpdate(DML.Record record);
Commitable toUpdate(Iterable<SObject> records);
Commitable toUpdate(DML.Records records);Example
update account;Account account = [SELECT Id, Name FROM Account LIMIT 1];
account.Name = 'Updated Name';
new DML()
.toUpdate(account)
.commitWork();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
Commitable toUpsert(SObject record);
Commitable toUpsert(DML.Record record);
Commitable toUpsert(Iterable<SObject> records);
Commitable toUpsert(DML.Records records);Example
upsert accounts;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
Commitable toDelete(Id recordId);
Commitable toDelete(SObject record);
Commitable toDelete(Iterable<Id> recordIds);
Commitable toDelete(List<SObject> records);Example
delete account;new DML()
.toDelete(accountId)
.commitWork();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
Commitable toUndelete(Id recordId);
Commitable toUndelete(SObject record);
Commitable toUndelete(Iterable<Id> recordIds);
Commitable toUndelete(List<SObject> records);Example
undelete account;new DML()
.toUndelete(deletedAccountId)
.commitWork();MERGE
toMerge
Merge duplicate records (placeholder implementation).
Signature
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
merge masterAccount duplicateAccount;new DML()
.toMerge(masterAccount, duplicateAccount)
.commitWork();PLATFORM EVENTS
toPublish
Publish platform events.
Signature
Commitable toPublish(SObject record);
Commitable toPublish(Iterable<SObject> records);Example
EventBus.publish(events);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
Commitable userMode();Example
new DML()
.toInsert(new Case(Subject = 'User Mode Case'))
.userMode()
.commitWork();systemMode
Execute DML operations bypassing user permissions.
Signature
Commitable systemMode();Example
new DML()
.toInsert(new Case(Subject = 'System Mode Case'))
.systemMode()
.commitWork();stripNotCreatableFields
Remove fields that the current user cannot create.
Signature
Commitable stripNotCreatableFields();Example
new DML()
.toInsert(task)
.systemMode()
.stripNotCreatableFields()
.commitWork();stripNotUpdatableFields
Remove fields that the current user cannot update.
Signature
Commitable stripNotUpdatableFields();Example
new DML()
.toUpdate(accounts)
.stripNotUpdatableFields()
.commitWork();SHARING MODE
withSharing
Execute DML operations enforcing sharing rules.
Signature
Commitable withSharing();Example
new DML()
.toInsert(accounts)
.withSharing()
.commitWork();withoutSharing
Execute DML operations bypassing sharing rules.
Signature
Commitable withoutSharing();Example
new DML()
.toInsert(accounts)
.withoutSharing()
.commitWork();CONFIGURATION
allowPartialSuccess
Allow some records to fail without rolling back successful operations.
Signature
Commitable allowPartialSuccess();Example
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
Commitable skipDuplicateRules();Example
new DML()
.toInsert(accounts)
.skipDuplicateRules()
.commitWork();options
Provide custom Database.DmlOptions for DML operations.
Signature
Commitable options(Database.DmlOptions options);Example
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
Commitable commitHook(DML.Hook callback);Example
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
void preview();Example
new DML()
.toInsert(account)
.toUpdate(contact)
.preview(); // Outputs debug information to logsEXECUTION
commitWork
Execute all registered DML operations in a single transaction.
Signature
void commitWork();Example
new DML()
.toInsert(account)
.toUpdate(contact)
.toDelete(leadId)
.commitWork();Record Manipulation
Field Assignment
Assign field values to records using the with method.
DML.Record(contact).with(Contact.Email, 'john@example.com')Relationship Handling
Handle parent-child relationships in DML operations.
DML.Record(contact).withRelationship(Contact.AccountId, account)DML.Record(contact).withRelationship(
Contact.AccountId,
Account.External_Id__c,
'EXT123'
)Factory Methods
DML.Record
Create a Record wrapper for single SObject manipulation.
Signature
Record Record(Id recordId);
Record Record(SObject record);Example
DML.Record record = DML.Record(sobjectInstance);
DML.Record recordById = DML.Record(recordId);DML.Records
Create a Records wrapper for multiple SObject manipulation.
Signature
Records Records(List<SObject> records);
Records Records(Iterable<Id> recordIds);Example
DML.Records records = DML.Records(sobjectList);
DML.Records recordsByIds = DML.Records(recordIdSet);