top of page
Writer's pictureA. Kumar

APEX DATABASE: DML

Updated: Sep 21, 2023


APEX DATABASE DML

What is DML?

● DML stands for Data Manipulation Language.

● Each DML statement accepts either a single or a list of subjects.

● DML limit of 150 statements per Apex transaction.


What are DML Operations?

● Insert

● Update

● Upsert

● Delete

● Undelete

What are DML Statements?

DML statements can be used to perform DML operations. But if even a single record error is out in the entire list of records then the entire operation is rollbacked.


1. Insert:

  1. Insert operation is used to create new records in the Database. You can create records of any Standard or Custom object using the Insert DML statement.

  2. Ex: Inserting Single Account :

Account newAcct = new Account(name = ‘test’); insert newAcct;

Inserting List<Account> :

List<Account> accountList = new List<Account>(); accountList.add( new Account( name= ‘ Test 1’)); accountList.add( new Account( name= ‘ Test 2’)); Insert accountList;

2. Update:

  1. The update DML operation modifies one or more existing sObject records.

  2. Ex : Updating One Account : Account a = new Account(Name=’Test’); insert(a); Account acc = [ Select Name, BillingCity From Account Where Id = : a.Id]; acc.BillingCity = ‘Raipur’; Update acc;

3. Upsert:

  1. Upsert Operation is used to perform an update operation and if the records to be updated are not present in the database, then create new records as well.

  2. The upsert statement matches the sObjects with existing records by comparing the values of one field. If you don’t specify a field when calling this statement, the upsert statement uses the sObject’s ID to match the sObject with existing records in Salesforce.

    1. upsert sObject​​[opt_field]

  3. Upsert uses the sObject record's primary key (the ID), an id Lookup field, or an external ID field to determine whether it should create a record or update an existing one:

    1. If the key isn’t matched, a new object record is created.

    2. If the key is matched once, the existing object record is updated.

    3. If the key is matched multiple times, an error is generated and the object record isn’t inserted or updated

  4. Ex :

List<Account> accountLIst= new List<Account>();

for (Integer i = 0; i < 10; i++) {

Account acc = newAccount (name = 'Test' +i, external_id__c = '1234' +i);

accountLIst.add(objcust);

}

upsert accountLIst external_id__c ;


4. Delete:

  1. The delete DML operation deletes one or more existing sObject records.

  2. Deleted records are not deleted permanently from the Lightning platform but are placed in the recycle bin for 15 days from where they can be restored.

  3. Ex : Account[] accounts= [SELECT Id, Name FROM Account WHERE Name =’Test’]; Delete accounts;

5. Undelete :

  1. The undelete DML operation restores one or more existing sObject records, from your organization’s Recycle Bin.

  2. Ex :

Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Test’ ALL ROWS];

undelete savedAccts;


6. Merge :

  1. The merge statement merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

  2. The first parameter represents the master record into which the other records are to be merged. The second parameter represents one or two other records that should be merged and deleted. You can pass these other records into the merge statement as a single sObject record or ID, or as a list of two sObject records or IDs.

  3. Ex:

List<Account> acc = new List<Account>{new Account(name='Test 1.'),new Account(name='Test 2')};

insert ls;

Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Test 1' LIMIT 1];

Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = ‘Test 2' LIMIT 1];

merge masterAcct mergeAcct;


DML Statements vs. Database Class Methods

DML Statements

Database Class Methods

Partial updates are not allowed. For example, if you have 100 records in the list, then either all the records will be updated or none.

Example :

List<Account> acctList = new List<Account>();

acctList.add(new Account(Name='Test 1'));

Insert acctList;

Partial updates are allowed. You can specify the parameter in the Database method as true or false to allow the partial update and false to not allow the same.

Example :

List<Account> acctList = new List<Account>();

acctList.add(new Account(Name='Test 1'));

Database.SaveResult[] srList = Database.insert(acctList, false);

Convert Lead Not Available in DML Statements

Convert Lead Operation is only available in the Database class.

DML Statements

● Insert acctList;

● Update acctLList;


Database Method

● List<Database.SaveResult> results = Database.insert(acctList, false);

● List<Database.SaveResult> results = Database.update(acctList, false);


What is Mixed DML Error?

● Two sObject(Setup & Non-Setup) that you're using in your code can not be mixed in the same transaction.

-> There are 2 kinds of sObjects in Salesforce.

Non-Setup: Account, opportunity, etc

Setup: User, groups, etc

● To avoid these errors, we should perform DML operations on standard/custom objects in different transactions.


Best Practices for DML

● With DML on SObjects, it’s best to construct new instances and only update the fields you wish to modify without querying other fields. If your query fields are other than the fields you wish to update, you may revert queried field values that could have changed between the query and the DML.


Key Point

● If you execute DML operations within an anonymous block, they execute using the current user’s object and field-level permissions.


Happy!! to help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.


Blog Credit:

A. Kumar

Salesforce Developer

Avenoir Technologies Pvt. Ltd.


Reach us: team@avenoir.ai

Recent Posts

See All

Comments


bottom of page