Blog

DML Operations In Salesforce

  • (5.0)
  • | 355 Ratings

DML statements are called as ‘Data Manipulator/ Manipulation language’. We can make the changes in the database and commit the same by using statements like Insert - Update - Delete. DML statements are so essential, that without this you can’t make any changes to records in Database / Object. By using database queries like SOQL/SOSL, we can only fetch data.


  • Trigger + Class = Logic
  • SOQL + SOSL = Fetch the Data
  • DML → Data Manipulation Language.

Syntax For DML:  DML_statement sObject (one record);

Eg: Insert p;

Insert ListOfRecords;



Sample Program#


//create variable
Lead myLead = new Lead();

//Load data
myLead.FirstName = ‘John’;
myLead.LastName = ‘Craig’;
myLead.Company = ‘Google’;

//perform DML
Insert myLead;

Once the insert DML operation is performed, the inserted record gets an ‘Record Id’ which we can use hereafter for addressing the record / updating the record / fetching the record.


Get ahead in your career by learning Salesforce through Mindmajix Salesforce Training.

 

How to perform DML operation in Salesforce?


Sample Program#1: APEX Class A  - 14 Feb


Scenario: There is a request to create a lead from the info we are getting by email.


Code:

Lead myLead = new Lead();
myLead.FirstName = ‘Stephen’;
myLead.LastName = ‘	Burgers’;
myLead.Company = ‘ICICI’;
myLead.Phone = ‘8276356198’;
Insert myLead;

Note: By, the way Lead is a standard object existing in the Salesforce Environment, we can go and check whether the record is inserted or not.


Checkout SalesForce Tutorial


Sample Program#2: APEX Class B - 20 Mar


Scenario: There is a request to modify company name of lead. This request came from website connected to Salesforce / email request.


Hint: Get Unique ID/Reference of that record present in the database.


Phone Number is Unique so we can update the record using Phone Number of the Existing Record.


#Code:

Lead l2 = new Lead();
l2 = [SELECT id, company FROM Lead WHERE phone = ‘8276356198’ ];
l2.Company = ‘philips’;
Update l2;

Note: Now, we can check whether the record got modified or not as, we have updated the existing record with Unique Phone Number.


Sample Program#3: APEX Class C - 25 Apr


Scenario: We got a requirement to delete the record by some VFP.


Hint: Get Unique ID/Reference of that record present in the database.

Lead l2 = new Lead();
l2 = [SELECT id FROM Lead WHERE phone = ‘8276356198’ ];
Delete l2;

Note: Now, we can check for the record whether it is deleted or not.


Sample Program#4: APEX Class D - 10 May


Scenario: We got a requirement to un-delete the record, by process builder.


Hint: Get Unique ID/Reference of that record present in the recycle bin.

Lead l2 = new Lead();
l2 = [SELECT id FROM Lead WHERE phone = ‘8276356198’ ALL ROWS ];
Undelete l2;

 

How to process bulk Records with DML operation?


Sample Program#5: APEX Class D - 1 Jul


Scenario: We got a requirement where in one shot we need to update and well as Insert multiple records.


UPSERT = Update + Insert


Code#

//Creating record
Lead l1 = new Lead();
l1.FirstName = ‘Elle’;
l1.LastName = ‘Charms’;
l1.Company = ‘ICICI’;
Insert l1;

//Updating the record
Lead l2 = new Lead();
l2= [SELECT id FROM Lead WHERE Phone = ‘8276356198’];
l2.Company = ‘Microsoft’;

//List Creation to add both
List<lead> MyList = new List<Lead>();
MyList.add(l1);
MyList .add(l2);
Upsert MyList;

Note: At a time we can work only on one Object.

 

How to Insert multiple records at a time with DML operation?


Sample Program#6: Insert multiple records at a time.


Code#

//Creating record l1
Lead l1 = new Lead();
l1.FirstName = ‘Elle’;
l1.LastName = ‘Charms’;
l1.Company = ‘ICICI’;
Insert l1;

//Creating record l2
Lead l2 = new Lead();
l2.FirstName = ‘Belle’;
l2.LastName = ‘Dreams’;
l2.Company = ‘ICICI’;
Insert l2;

.
.
.
….

//we can add upto ‘n’ records

//Logic to insert multiple records
list<Lead> InsertedRecords = New List<Lead>();
InsertedRecords.add(l1);
InsertedRecords.add(l2);
Insert InsertedRecords;

 

How to perform Bulk DML operation in Salesforce?


Sample Program#1: Bulk DML Operation


Scenario: In Lead Object, we got a scenario where we need to fetch all records having company “Flipkart”, is to be converted to “Walmart”.


Checkout Salesforce Interview Questions

#Code:

//Fetch all records
List<Lead> ListOfFlipkart = New List<Lead>();
ListOfFlipkart = [SELECT Company FROM Lead WHERE Company= ‘Flipkart’];
system.debug(‘ListOfFlipkart =’ + ListOfFlipkart);

List<Lead> ListOfUpdatedRecords = New List<Lead>();

//Process all records with new company name
for(lead l : ListOfFlipkart ){

	l.Company = ‘Walmart’;

	ListOfUpdatedRecords.add(l);
}

Update ListOfUpdatedRecords;

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


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


Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The Author

Arogyalokesh is a Senior Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Blockchain, Salesforce, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close