Insert
Insert new records into the database.
toInsert
Register records for insertion. The actual DML is executed when commitWork() is called.
Signature
Commitable toInsert(SObject record);
Commitable toInsert(DML.Record record);
Commitable toInsert(Iterable<SObject> records);
Commitable toInsert(DML.Records records);Example
Account account = new Account(Name = 'My Account');
Contact contact = new Contact(LastName = 'Doe');
new DML()
.toInsert(account)
.toInsert(DML.Record(contact)
.withRelationship(Contact.AccountId, account)
)
.systemMode()
.withoutSharing()
.commitWork();Single Record
Signature
Commitable toInsert(SObject record);Standard DML
insert new Account(Name = 'My Account');DML Lib
new DML()
.toInsert(new Account(Name = 'My Account'))
.commitWork();Parent Relationship
Handle parent-child relationships automatically.
Signature
Commitable toInsert(DML.Record record);Standard DML
Account account = new Account(Name = 'Parent Account');
insert account;
Contact contact = new Contact(
FirstName = 'John',
LastName = 'Doe',
AccountId = account.Id
);
insert contact;DML Lib
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();With Field
Set field values dynamically using DML.Record.
Signature
Commitable toInsert(DML.Record record);Standard DML
Contact contact = new Contact(LastName = 'Doe');
contact.Email = 'john@example.com';
insert contact;DML Lib
Contact contact = new Contact(LastName = 'Doe');
new DML()
.toInsert(DML.Record(contact).with(Contact.Email, 'john@example.com'))
.commitWork();Multiple Records
Signature
Commitable toInsert(Iterable<SObject> records);
Commitable toInsert(DML.Records records);Standard DML
List<Account> accounts = new List<Account>{
new Account(Name = 'Account 1'),
new Account(Name = 'Account 2')
};
insert accounts;DML Lib
List<Account> accounts = new List<Account>{
new Account(Name = 'Account 1'),
new Account(Name = 'Account 2')
};
new DML()
.toInsert(accounts)
.commitWork();Parent Relationship
Signature
Commitable toInsert(DML.Records records);Standard DML
Account account = new Account(Name = 'My Account');
insert account;
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
for (Contact c : contacts) {
c.AccountId = account.Id;
}
insert contacts;DML Lib
Account account = new Account(Name = 'My Account');
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
new DML()
.toInsert(account)
.toInsert(DML.Records(contacts).withRelationship(Contact.AccountId, account))
.commitWork();With Field
Set field values dynamically for multiple records using DML.Records.
Signature
Commitable toInsert(DML.Records records);Standard DML
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
for (Contact c : contacts) {
c.LeadSource = 'Web';
}
insert contacts;DML Lib
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
new DML()
.toInsert(DML.Records(contacts).with(Contact.LeadSource, 'Web'))
.commitWork();insertImmediately
Insert records immediately and return the operation result without calling commitWork().
Signature
OperationResult insertImmediately(SObject record);
OperationResult insertImmediately(DML.Record record);
OperationResult insertImmediately(List<SObject> records);
OperationResult insertImmediately(DML.Records records);TIP
All DML settings configured on the DML instance (such as userMode(), systemMode(), withSharing(), withoutSharing(), allowPartialSuccess()) are inherited when executing insertImmediately.
Single Record
Signature
OperationResult insertImmediately(SObject record);Standard DML
Account account = new Account(Name = 'My Account');
Database.SaveResult result = Database.insert(account);DML Lib
Account account = new Account(Name = 'My Account');
DML.OperationResult result = new DML().insertImmediately(account);Parent Relationship
Handle parent-child relationships automatically.
Signature
OperationResult insertImmediately(DML.Record record);Standard DML
Account account = new Account(Name = 'Parent Account');
Database.SaveResult accountResult = Database.insert(account);
Contact contact = new Contact(
FirstName = 'John',
LastName = 'Doe',
AccountId = account.Id
);
Database.SaveResult contactResult = Database.insert(contact);DML Lib
Account account = new Account(Name = 'Parent Account');
new DML().insertImmediately(account);
Contact contact = new Contact(FirstName = 'John', LastName = 'Doe');
DML.OperationResult result = new DML()
.insertImmediately(DML.Record(contact).withRelationship(Contact.AccountId, account));With Field
Set field values dynamically using DML.Record.
Signature
OperationResult insertImmediately(DML.Record record);Standard DML
Contact contact = new Contact(LastName = 'Doe');
contact.Email = 'john@example.com';
Database.SaveResult result = Database.insert(contact);DML Lib
Contact contact = new Contact(LastName = 'Doe');
DML.OperationResult result = new DML()
.insertImmediately(DML.Record(contact).with(Contact.Email, 'john@example.com'));Multiple Records
Signature
OperationResult insertImmediately(List<SObject> records);
OperationResult insertImmediately(DML.Records records);Standard DML
List<Account> accounts = new List<Account>{
new Account(Name = 'Account 1'),
new Account(Name = 'Account 2')
};
List<Database.SaveResult> results = Database.insert(accounts);DML Lib
List<Account> accounts = new List<Account>{
new Account(Name = 'Account 1'),
new Account(Name = 'Account 2')
};
DML.OperationResult result = new DML().insertImmediately(accounts);Parent Relationship
Signature
OperationResult insertImmediately(DML.Records records);Standard DML
Account account = new Account(Name = 'My Account');
Database.SaveResult accountResult = Database.insert(account);
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
for (Contact c : contacts) {
c.AccountId = account.Id;
}
List<Database.SaveResult> results = Database.insert(contacts);DML Lib
Account account = new Account(Name = 'My Account');
new DML().insertImmediately(account);
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
DML.OperationResult result = new DML()
.insertImmediately(DML.Records(contacts).with(Contact.AccountId, account));With Field
Set field values dynamically for multiple records using DML.Records.
Signature
OperationResult insertImmediately(DML.Records records);Standard DML
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
for (Contact c : contacts) {
c.LeadSource = 'Web';
}
List<Database.SaveResult> results = Database.insert(contacts);DML Lib
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Doe'),
new Contact(LastName = 'Smith')
};
DML.OperationResult result = new DML()
.insertImmediately(DML.Records(contacts).with(Contact.LeadSource, 'Web'));