Skip to content

DML_Transaction

Class · Group: DML

apex
global virtual inherited sharing class DML_Transaction

Known Derived Types: API_Base, API_CallCurrentOrg, API_Inbound, API_Outbound

Transaction engine for managing complex DML operations across multiple SObjects. Handles dependency ordering via topological sort, parent-child relationship resolution, and lifecycle hooks for extensibility.

Example

apex
DML_Transaction aTransaction = new DML_Transaction();
aTransaction.registerInsert(account);
aTransaction.registerInsert(contact, Contact.AccountId, account);
aTransaction.commitWork();

See Also: DML_Builder


Methods

MethodDescription
global virtual void commitWork()Commits all registered DML operations using inherited sharing.
global virtual void commitWork(Boolean enforceSharing)Commits all registered DML operations with configurable sharing enforcement.
global void doDelete(List<SObject> records)Registers multiple records for deletion.
global void doDelete(SObject record)Registers a record for deletion.
global void doInsert(List<SObject> records)Registers multiple records for insertion.
global void doInsert(SObject record)Registers a record for insertion.
global void doInsert(SObject child, SObjectField relationshipField, SObject parent)Registers a child record for insertion with a relationship to a parent record.
global void doSave(SObject record)Saves a record by inserting (if no Id) or updating (if Id exists).
global void doUndelete(List<SObject> records)Registers multiple records for undelete.
global void doUndelete(SObject record)Registers a record for undelete.
global void doUpdate(List<SObject> records)Registers multiple records for update.
global void doUpdate(SObject record)Registers a record for update.
global void doUpdate(SObject record, SObjectField relationshipField, SObject parent)Registers a record for update with a relationship to a parent record.
global void doUpsert(List<SObject> records)Registers multiple records for upsert without an external ID field.
global void doUpsert(List<SObject> records, SObjectField externalIdField)Registers multiple records for upsert using a specific external ID field.
global void doUpsert(SObject record)Registers a record for upsert without an external ID field.
global void doUpsert(SObject record, SObjectField externalIdField)Registers a record for upsert using a specific external ID field.
global void doUpsert(SObject child, SObjectField relationshipField, SObjectField externalIdField, SObject parent)Registers a child record for upsert with a parent relationship and external ID field.
global virtual void onCommitWorkFinished(Boolean wasSuccessful)Hook called after the commit process finalizes, indicating success or failure.
global virtual void onCommitWorkFinishing()Hook called after DML operations but before finalization.
global virtual void onCommitWorkStarting()Hook called before the commit process starts.
global virtual void onDMLFinished()Hook called after DML operations complete.
global virtual void onDMLStarting()Hook called before DML operations start.
global void setAccessLevel(AccessLevel accessLevel)Sets the AccessLevel that will be passed to every DML operation via the DML_SharingProxy three-arg methods.
global void setAllowPartial(Boolean allow)Sets whether partial success is allowed for DML operations.
global void setSuppressLogging(Boolean suppress)Sets whether to suppress LOG_Builder error logging for partial DML results.

commitWork

apex
global virtual void commitWork()

Commits all registered DML operations using inherited sharing.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork();
apex
global virtual void commitWork(Boolean enforceSharing)

Commits all registered DML operations with configurable sharing enforcement.

Parameters

ParameterTypeDescription
enforceSharingBooleanIf true, enforces sharing; if false, bypasses sharing; if null, inherits sharing.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork(false);

doDelete

apex
global void doDelete(List<SObject> records)

Registers multiple records for deletion.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to delete.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doDelete(obsoleteRecords);
dmlTransaction.commitWork();
apex
global void doDelete(SObject record)

Registers a record for deletion.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to delete.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doDelete(obsoleteRecord);
dmlTransaction.commitWork();

doInsert

apex
global void doInsert(List<SObject> records)

Registers multiple records for insertion.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to insert.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new List<Account>{new Account(Name = 'A'), new Account(Name = 'B')});
dmlTransaction.commitWork();
apex
global void doInsert(SObject record)

Registers a record for insertion.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to insert.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork();
apex
global void doInsert(SObject child, SObjectField relationshipField, SObject parent)

Registers a child record for insertion with a relationship to a parent record. The child's relationship field will be populated with the parent's Id after commit.

Parameters

ParameterTypeDescription
childSObjectThe child SObject to insert.
relationshipFieldSObjectFieldThe field linking the child to the parent (e.g., Contact.AccountId).
parentSObjectThe parent SObject.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
Account parent = new Account(Name = 'Parent');
Contact child = new Contact(LastName = 'Child');
dmlTransaction.doInsert(parent);
dmlTransaction.doInsert(child, Contact.AccountId, parent);
dmlTransaction.commitWork();

doSave

apex
global void doSave(SObject record)

Saves a record by inserting (if no Id) or updating (if Id exists). Convenience method for mixed operations.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to save.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doSave(new Account(Name = 'Test'));
dmlTransaction.commitWork();

doUndelete

apex
global void doUndelete(List<SObject> records)

Registers multiple records for undelete.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to undelete.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUndelete(deletedAccounts);
dmlTransaction.commitWork();
apex
global void doUndelete(SObject record)

Registers a record for undelete.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to undelete.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUndelete(deletedAccount);
dmlTransaction.commitWork();

doUpdate

apex
global void doUpdate(List<SObject> records)

Registers multiple records for update.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to update.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(existingAccounts);
dmlTransaction.commitWork();
apex
global void doUpdate(SObject record)

Registers a record for update.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to update.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(existingAccount);
dmlTransaction.commitWork();
apex
global void doUpdate(SObject record, SObjectField relationshipField, SObject parent)

Registers a record for update with a relationship to a parent record.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to update.
relationshipFieldSObjectFieldThe field linking the record to the parent.
parentSObjectThe parent SObject whose Id will be set on the relationship field.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(contact, Contact.AccountId, newAccount);
dmlTransaction.commitWork();

doUpsert

apex
global void doUpsert(List<SObject> records)

Registers multiple records for upsert without an external ID field.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to upsert.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(accounts);
dmlTransaction.commitWork();
apex
global void doUpsert(List<SObject> records, SObjectField externalIdField)

Registers multiple records for upsert using a specific external ID field.

Parameters

ParameterTypeDescription
recordsListThe list of SObjects to upsert.
externalIdFieldSObjectThe external ID field for matching.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(accounts, Account.ExternalId__c);
dmlTransaction.commitWork();
apex
global void doUpsert(SObject record)

Registers a record for upsert without an external ID field. Uses the record's Id field for matching if present.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to upsert.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(account);
dmlTransaction.commitWork();
apex
global void doUpsert(SObject record, SObjectField externalIdField)

Registers a record for upsert using a specific external ID field. All records of the same SObjectType within a single transaction must use the same external ID field.

Parameters

ParameterTypeDescription
recordSObjectThe SObject to upsert.
externalIdFieldSObjectFieldThe external ID field for matching.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(account, Account.ExternalId__c);
dmlTransaction.commitWork();
apex
global void doUpsert(SObject child, SObjectField relationshipField, SObjectField externalIdField, SObject parent)

Registers a child record for upsert with a parent relationship and external ID field.

Parameters

ParameterTypeDescription
childSObjectThe child SObject to upsert.
relationshipFieldSObjectFieldThe field linking the child to the parent.
externalIdFieldSObjectFieldThe external ID field for matching.
parentSObjectThe parent SObject.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(child, Contact.AccountId, Contact.ExternalId__c, parent);
dmlTransaction.commitWork();

onCommitWorkFinished

apex
global virtual void onCommitWorkFinished(Boolean wasSuccessful)

Hook called after the commit process finalizes, indicating success or failure.

Parameters

ParameterTypeDescription
wasSuccessfulBooleanWhether the commit was successful.

Example

apex
global override void onCommitWorkFinished(Boolean wasSuccessful)
{
    super.onCommitWorkFinished(wasSuccessful);
    if(wasSuccessful)
    {
        // custom post-commit logic
    }
}

onCommitWorkFinishing

apex
global virtual void onCommitWorkFinishing()

Hook called after DML operations but before finalization.

Example

apex
global override void onCommitWorkFinishing()
{
    super.onCommitWorkFinishing();
    // custom pre-finalization logic
}

onCommitWorkStarting

apex
global virtual void onCommitWorkStarting()

Hook called before the commit process starts.

Example

apex
global override void onCommitWorkStarting()
{
    super.onCommitWorkStarting();
    // custom pre-commit logic
}

onDMLFinished

apex
global virtual void onDMLFinished()

Hook called after DML operations complete.

Example

apex
global override void onDMLFinished()
{
    super.onDMLFinished();
    // custom post-DML logic
}

onDMLStarting

apex
global virtual void onDMLStarting()

Hook called before DML operations start.

Example

apex
global override void onDMLStarting()
{
    super.onDMLStarting();
    // custom pre-DML logic
}

setAccessLevel

apex
global void setAccessLevel(AccessLevel accessLevel)

Sets the AccessLevel that will be passed to every DML operation via the DML_SharingProxy three-arg methods. When null, the proxy resolves to the flag-driven default (UserModeDml_Enabled).

Parameters

ParameterTypeDescription
accessLevelAccessLevelThe AccessLevel to apply; null to inherit the flag-driven default.

Example

apex
DML_Transaction transaction = new DML_Transaction();
transaction.setAccessLevel(AccessLevel.SYSTEM_MODE);
transaction.doInsert(records);
transaction.commitWork();

setAllowPartial

apex
global void setAllowPartial(Boolean allow)

Sets whether partial success is allowed for DML operations. When enabled, failed records do not cause the entire transaction to roll back.

Parameters

ParameterTypeDescription
allowBooleanTrue to allow partial success, false for all-or-nothing (default).

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.setAllowPartial(true);
dmlTransaction.doInsert(records);
dmlTransaction.commitWork();

setSuppressLogging

apex
global void setSuppressLogging(Boolean suppress)

Sets whether to suppress LOG_Builder error logging for partial DML results. When enabled, partial failures are not logged.

Parameters

ParameterTypeDescription
suppressBooleanTrue to suppress logging, false to allow logging (default).

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.setAllowPartial(true);
dmlTransaction.setSuppressLogging(true);
dmlTransaction.doInsert(records);
dmlTransaction.commitWork();

Constructors

ConstructorDescription
global DML_Transaction()Initializes a new transaction with empty collections for managing DML operations.

DML_Transaction()

apex
global DML_Transaction()

Initializes a new transaction with empty collections for managing DML operations.

Example

apex
DML_Transaction dmlTransaction = new DML_Transaction();