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:

Salesforce DML Operations

Insert Operation

The DML insert operation is used to establish new data in the database. The data is added for any custom or standard object.

Want to enhance your skills in dealing with the worlds best CRM, enroll in our Salesforce Training 

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.

Checkout SalesForce Tutorial

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

Subscribe to our youtube channel to get new updates..!

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.

Checkout Salesforce Interview Questions

Undelete Operation

This DML operation is used to restore the existing records of a standard object or a custom object.

Syntax

Undelete vObject | Name

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 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. 

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. 

In the next topic, we will discuss in detail about “Governor Limits in Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts