DML_Transaction
Class · Group: DML
global virtual inherited sharing class DML_TransactionKnown 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.
Since: 1.0
Example:
DML_Transaction aTransaction = new DML_Transaction();
aTransaction.registerInsert(account);
aTransaction.registerInsert(contact, Contact.AccountId, account);
aTransaction.commitWork();See Also: DML_Builder
Methods
| Method | Description |
|---|---|
| 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 DML_Transaction() | Initializes a new transaction with empty collections for managing DML operations. |
| 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, SObject 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, SObject 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, SObject 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, SObject externalIdField) | Registers a record for upsert using a specific external ID field. |
| global void doUpsert(SObject child, SObject relationshipField, SObject 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. |
Method Details
DML_Transaction
global DML_Transaction()Initializes a new transaction with empty collections for managing DML operations.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();commitWork
global virtual void commitWork()Commits all registered DML operations using inherited sharing.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork();commitWork
global virtual void commitWork(Boolean enforceSharing)Commits all registered DML operations with configurable sharing enforcement.
Parameters:
enforceSharing(Boolean) - If true, enforces sharing; if false, bypasses sharing; if null, inherits sharing.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork(false);doDelete
global void doDelete(List<SObject> records)Registers multiple records for deletion.
Parameters:
records(List) - The list of SObjects to delete.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doDelete(obsoleteRecords);
dmlTransaction.commitWork();doDelete
global void doDelete(SObject record)Registers a record for deletion.
Parameters:
record(SObject) - The SObject to delete.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doDelete(obsoleteRecord);
dmlTransaction.commitWork();doInsert
global void doInsert(List<SObject> records)Registers multiple records for insertion.
Parameters:
records(List) - The list of SObjects to insert.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new List<Account>{new Account(Name = 'A'), new Account(Name = 'B')});
dmlTransaction.commitWork();doInsert
global void doInsert(SObject record)Registers a record for insertion.
Parameters:
record(SObject) - The SObject to insert.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doInsert(new Account(Name = 'Test'));
dmlTransaction.commitWork();doInsert
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:
child(SObject) - The child SObject to insert.relationshipField(SObjectField) - The field linking the child to the parent (e.g., Contact.AccountId).parent(SObject) - The parent SObject.
Since: 1.0
Example:
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
global void doSave(SObject record)Saves a record by inserting (if no Id) or updating (if Id exists). Convenience method for mixed operations.
Parameters:
record(SObject) - The SObject to save.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doSave(new Account(Name = 'Test'));
dmlTransaction.commitWork();doUndelete
global void doUndelete(List<SObject> records)Registers multiple records for undelete.
Parameters:
records(List) - The list of SObjects to undelete.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUndelete(deletedAccounts);
dmlTransaction.commitWork();doUndelete
global void doUndelete(SObject record)Registers a record for undelete.
Parameters:
record(SObject) - The SObject to undelete.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUndelete(deletedAccount);
dmlTransaction.commitWork();doUpdate
global void doUpdate(List<SObject> records)Registers multiple records for update.
Parameters:
records(List) - The list of SObjects to update.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(existingAccounts);
dmlTransaction.commitWork();doUpdate
global void doUpdate(SObject record)Registers a record for update.
Parameters:
record(SObject) - The SObject to update.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(existingAccount);
dmlTransaction.commitWork();doUpdate
global void doUpdate(SObject record, SObjectField relationshipField, SObject parent)Registers a record for update with a relationship to a parent record.
Parameters:
record(SObject) - The SObject to update.relationshipField(SObjectField) - The field linking the record to the parent.parent(SObject) - The parent SObject whose Id will be set on the relationship field.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpdate(contact, Contact.AccountId, newAccount);
dmlTransaction.commitWork();doUpsert
global void doUpsert(List<SObject> records)Registers multiple records for upsert without an external ID field.
Parameters:
records(List) - The list of SObjects to upsert.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(accounts);
dmlTransaction.commitWork();doUpsert
global void doUpsert(List<SObject> records, SObjectField externalIdField)Registers multiple records for upsert using a specific external ID field.
Parameters:
records(List) - The list of SObjects to upsert.externalIdField(SObject) - The external ID field for matching.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(accounts, Account.ExternalId__c);
dmlTransaction.commitWork();doUpsert
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:
record(SObject) - The SObject to upsert.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(account);
dmlTransaction.commitWork();doUpsert
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:
record(SObject) - The SObject to upsert.externalIdField(SObjectField) - The external ID field for matching.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(account, Account.ExternalId__c);
dmlTransaction.commitWork();doUpsert
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:
child(SObject) - The child SObject to upsert.relationshipField(SObjectField) - The field linking the child to the parent.externalIdField(SObjectField) - The external ID field for matching.parent(SObject) - The parent SObject.
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.doUpsert(child, Contact.AccountId, Contact.ExternalId__c, parent);
dmlTransaction.commitWork();onCommitWorkFinished
global virtual void onCommitWorkFinished(Boolean wasSuccessful)Hook called after the commit process finalizes, indicating success or failure.
Parameters:
wasSuccessful(Boolean) - Whether the commit was successful.
Since: 1.0
Example:
global override void onCommitWorkFinished(Boolean wasSuccessful)
{
super.onCommitWorkFinished(wasSuccessful);
if(wasSuccessful)
{
// custom post-commit logic
}
}onCommitWorkFinishing
global virtual void onCommitWorkFinishing()Hook called after DML operations but before finalization.
Since: 1.0
Example:
global override void onCommitWorkFinishing()
{
super.onCommitWorkFinishing();
// custom pre-finalization logic
}onCommitWorkStarting
global virtual void onCommitWorkStarting()Hook called before the commit process starts.
Since: 1.0
Example:
global override void onCommitWorkStarting()
{
super.onCommitWorkStarting();
// custom pre-commit logic
}onDMLFinished
global virtual void onDMLFinished()Hook called after DML operations complete.
Since: 1.0
Example:
global override void onDMLFinished()
{
super.onDMLFinished();
// custom post-DML logic
}onDMLStarting
global virtual void onDMLStarting()Hook called before DML operations start.
Since: 1.0
Example:
global override void onDMLStarting()
{
super.onDMLStarting();
// custom pre-DML logic
}setAccessLevel
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:
accessLevel(AccessLevel) - The AccessLevel to apply; null to inherit the flag-driven default.
Since: 1.0
Example:
DML_Transaction transaction = new DML_Transaction();
transaction.setAccessLevel(AccessLevel.SYSTEM_MODE);
transaction.doInsert(records);
transaction.commitWork();setAllowPartial
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:
allow(Boolean) - True to allow partial success, false for all-or-nothing (default).
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.setAllowPartial(true);
dmlTransaction.doInsert(records);
dmlTransaction.commitWork();setSuppressLogging
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:
suppress(Boolean) - True to suppress logging, false to allow logging (default).
Since: 1.0
Example:
DML_Transaction dmlTransaction = new DML_Transaction();
dmlTransaction.setAllowPartial(true);
dmlTransaction.setSuppressLogging(true);
dmlTransaction.doInsert(records);
dmlTransaction.commitWork();