Introduction to Salesforce DML
The full form of DML is Data Manipulation Language and through DML Statements we can implement DML operations in the salesforce. DML is essential in Apex, as every business requires some changes and modifications regularly. Database Methods are the system methods which are also used to carry out DML operations. Database methods are more flexible than the DML statements. The data manipulation operations like insertion, deletion, updation are supported in salesforce. The DML operations are implemented in the following way:
We can perform 6 DML operations they are
- Insert.
- Update.
- Delete.
- UpSert.
- Merge and
- Restore.
Salesforce DML Operations
Insert Operation
The DML insert operation is used to establish new data in the database.The INSERT statement can be used to create new records for standard and custom objects in Salesforce.Developers can use the INSERT statement to insert a single record or a list of records at once.
Example:
Student newStud= new Student(Stud_Name=’Rakesh’);
Try {
Insert newStud;
} catch(DmlException d) {
Exception Code
}
The above code tells us how to insert a new student name. The try and catch block is used to check whether a student with name Rakesh is already present in the database or not.
Delete Operation
The delete operation is used to delete the existing records of a standard object or a custom object. The records can be anything like Student details, employee details, etc. Example for this deletion operation is as follows:
Student[] OldStuds= [SELECT Stud_ID , Stud_Name FROM Student WHERE Stud_Fee<10000];
Try{
Delete OldStuds;
} catch (DmlException d) {
Exception Code
}
The above code deletes the records of the students who have fees lesser than 10000.
Update Operation
This DML operation updates or modifies the existing records of a standard object or a custom object. The following example shows how we can update an existing record:
Student s = new Student(Name=” Rakesh”)
insert(s)
Student myStud = [ SELECT Stud_ID, Stud_Name, Stud_Fee FROM Student WHERE Stud_ID= s.Stud_ID];
myStud.Fee=’10000’;
try{
Update myStud;
} catch (DmlException d) {
Exception Code
}
Upsert Operation
This DML operation is used to update the existing record and creates a new record in a single statement.
Example:
Upsert vobject[optional_field]
Upsert vobject[] [optional_field}
The upsert statement suits the sObjects with available records by equating values of one domain. While invoking the Upsert statement if we don’t mention the domain it uses the ID of the sObject to compare the sObject with the available records in the salesforce. If you specify the ID, you can use it for comparing.
For custom objects, we should mention a custom field labelled as external ID. For Standard objects, we have to mention a field which has the idLookup attribute determined to be true.
How Upsert selects Update or Insert
- Upsert inserts a new record or updates an available record based on the ID of the sObject. Conditions for Update or Insert:
- A new record is inserted, when the key is not similar
- The available record is not updated when the key is not similar.
- When the key is matched several times, an error will be generated and the record of the object cannot be updated or inserted.
Undelete Operation
This DML operation is used to restore the existing records of a standard object or a custom object.
Syntax
Undelete vObject | Name
Undelete sObject | ID
Undelete sObject[] | ID[]
Example
Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS]; try { undelete savedAccts; } catch (DmlException e) { // Process exception here }
Merge Operation
This DML Operation is used to merge three records of the same object into one record. Merge operation is used to combine the records.
Syntax:
Merge vObject vObject
merge sObject sObject
merge sObject sObject[]
merge sObject ID
merge sObject ID[]
Example
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) {
// Process exception here
}
Merge vObject ID
The first argument portrays the superior record to which the other records are merged. The second argument portrays one or two records that are to be merged and after that, they can be deleted.
Different ways to perform DML operations.
- By using DML Statements
- By using Database Class.
By using DML Statements.
List <Account> accList = new List<Account>(); accList.add (new Account(Name='Account1')); accList.add(new Account(Name='Account2')); insert accList;
By using Database Class.
List <Account> accList = new List<Account>(); accList.add (new Account(Name='Account1')); accList.add(new Account(Name='Account2')); Database.SaveResult[] Sr = Database.insert(acctList,false);
As shown above, there is one difference between the two operations. In the database class method, we can specify whether to allow partial processing of the records if any errors are encountered by paring the boolean values as a parameters to Database.insert.
- If we give the parameter as ‘True’. if any error occurs it doesn’t allow the operation to continue.
- If we give the parameter as ‘false’, the remaining DML operations can still succeed. Where as insert in DML if any one of the record fails the total operation is discarded.
Conclusion
In salesforce, DML operations are performed to change the data according to our requirements. Dml operations play a key role in handling and formatting the data. DML plays a key role in Apex as it deals with crucial operations of the database.