Apex DML Operations!
Performing data manipulation language (DML) operations is essential in Salesforce development.
You can use Apex DML statements or the methods provided by the Database class to insert, update, merge, delete, and undelete data in Salesforce.
Here are the key DML statements available in Apex:
Insert Statement:
The insert DML operation allows you to add one or more sObjects (e.g., accounts, contacts) to your Salesforce organization's data.
Example:
The following example demonstrates inserting a new account named 'Acme':
Account newAcct = new Account(name = 'Acme');
try {
insert newAcct;
} catch (DmlException e) {
// Handle exception here
}
Update Statement:
The update DML operation enables the modification of existing sObject records, such as accounts or contacts, within your organization's data.
Example:
The following example updates the BillingCity field of an account named 'Acme':
Account a = new Account(Name='Acme2');
insert(a);
Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :a.Id];
myAcct.BillingCity = 'San Francisco';
try {
update myAcct;
} catch (DmlException e) {
// Handle exception here
}
Upsert Statement:
The upsert DML operation combines record creation and updating within a single statement.
It determines whether to create new records or update existing ones based on a specified field.
If no field is specified, the ID field is used as the default identifier.
Example:
The following example demonstrates upserting a list of accounts:
List<Account> acctList = new List<Account>();
// Fill the accounts list with some accounts
try {
upsert acctList;
} catch (DmlException e) {
// Handle exception here
}
Delete Statement:
The delete DML operation allows the deletion of one or more existing sObject records, such as accounts or contacts, from your organization's data.
Example:
The following example deletes all accounts with the name 'DotCom':
Account[] doomedAccts = [SELECT Id, Name FROM Account
WHERE Name = 'DotCom'];
try {
delete doomedAccts;
} catch (DmlException e) {
// Handle exception here
}
Undelete Statement:
The undelete DML operation restores one or more existing sObject records from the Recycle Bin.
It allows you to bring back deleted accounts or contacts to your organization's data.
Example:
The following example undoes the deletion of an account named 'Universal Containers':
Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS];
try {
undelete savedAccts;
} catch (DmlException e) {
// Handle exception here
}
Merge Statement:
The merge statement combines up to three records of the same sObject type into one record.
It deletes the other records and re-parents any related records.
Note that this operation does not have a matching Database system method.
Example:
The following example merges two accounts named 'Acme Inc.' and 'Acme' into a single record:
List<Account> ls = new List<Account>{new Account(name='Acme Inc.'),new Account(name='Acme')};
insert ls;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
try {
merge masterAcct mergeAcct;
} catch (DmlException e) {
// Handle exception here
}
Follow Us