Quantcast
Friday , July 20 2018
Home / Apex / Apex Programming for beginners / DML Operations in detail

DML Operations in detail

DML Operations in detail:

Data manipulation language operations mainly consist of insertion of record and committing it to the database, but we can also do following operations.

  1. Delete
  2. Recover(UnDelete)
  3. Update
  4. Insert
  5. Upsert(update and insert in one operation)
  6. Merge(In case of duplicate Records)
  7. Converting Leads

 

Inserting and updating the records

Records can be inserted and updated in the database. Records can be created individually or in bulk using the apex collection.

 

For ex: To insert an account just type the below line of code

 

Insert Acc; (Where Acc is an account sObject)

 

Similarly to insert if we want to update a record we write below line of code

 

Update Acc;

 

Inserting Related record, where the relationship is already been established between the objects, they are associated together through a foreign key for example while creating a contact record we can specify its account by providing the account Id which is the foreign key in this scenario.

 

Ex:

Account parentContactAcnt = new Account(Name=”Account for Contact”);

Insert parentContactAcnt;   //// using the id generated from this account and provide as a foreign key.

 

ID idforForeignKey = parentContactAcnt.Id;

 

Contact newCon = new Contact(

Firstname = “Test”;

LastName = “Contact”;

Phone = “111.222.333”;

AccountId = idforForeignKey;

);

Insert newCon;

 

Updating related records, is a bit more tricky then inserting related records and therefore we have to make two separate update call to update the related records. For example if we are updating a contact and we also want to update its account information, then to update account we have to make a separate DML call, see below for more detail :-

 

Ex:

Account parentContactAcnt = new Account(Name=”Account for Contact”);

Insert parentContactAcnt;   //// using the id generated from this account and provide as a foreign key.

 

ID idforForeignKey = parentContactAcnt.Id;

 

Contact newCon = new Contact(

Firstname = “Test”;

LastName = “Contact”;

Phone = “111.222.333”;

AccountId = idforForeignKey;

);

Insert newCon;

 

newCon.Phone = “223.334.445”;

newCon.account. Industry = ‘Technology’;

 

update newCon;

update newCon.account;     //// Separate DMl call to update parent record

 

 

Records can be related to each other using external Id as well, but this relationship has to be defined beforehand to relate any records.

 

Next: Other DML Operations

About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also

MODAL Box

How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …

Salesforce Lightning Tutorial

How to fetch and display list of records in Lightning Component

How to fetch and display list of records in Lightning Component? In this post we …

Leave a Reply

Your email address will not be published. Required fields are marked *