Professional Documents
Culture Documents
DML Statements
DML Statements
===============
DML --> Data Manipulation Language.
By using DML Statements, we can perform the operations on one or more records in
the object at a time.
Apex provides the below 2 Ways to perform the DML operations on the Records.
1. INSERT
2. DELETE
3. UPDATE
4. UPSERT --> UPDATE + INSERT
5. UNDELETE
6. MERGE
1. Database.Insert()
2. Database.Update()
3. Database.Delete()
4. Database.UnDelete()
5. Database.Upsert()
6. Database.Merge()
7. Database.EmptyRecycleBin()
Governor Limits:
----------------
1. We can use max. of 150 DML Statements per Transaction.
If the user tries use more than 150 DML Statements within a
transaction, then Salesforce will abort the Transaction, and will raise an
exception "System.LimitExeption. Too Many DML Statements: 151".
Best Practices:
---------------
1. As a Best Practice, Always we have to avoid the usage of DML statements
inside
the "FOR Loop".
i.e. Don't use any DML Statements inside the FOR Loop. Use always
outside the FOR Loop.
INSERT Statement:
=================
By using this statement, we can insert one or more records into the object at a
time.
Syntax:
-------
Inserting Only One Record:
--------------------------
Insert <objectName>;
Inserting Multiple Records:
---------------------------
Insert <collectionObjectName>;
UseCase:
========
Write an apex program, to insert 200 Hiring Manager Records inside the
object.
Class Code:
-----------
public class DMLUtility
{
Public static void InsertBulkHRRecords()
{
List<Hiring_Manager__C> lstHRRecords = new List<Hiring_Manager__C>();
if(! lstHRRecords.isEmpty())
{
insert lstHRRecords;
}
}
}
Execution:
----------
DMLUtility.InsertBulkHRRecords();
3. DML Statements won't provides any Tracking Mechanism, to track the record
operation result.
Database Class:
===============
--> Perform all the DML operations on the object records.
--> It provides the various Static methods
Database.Insert()
Database.Update()
Database.Delete()
Database.UnDelete()
Database.Upsert()
Database.Merge()
Database.EmptyRecycleBin()
--> Database Class Methods will overcome all the Drawbacks of all the DML
Statements.
Database.Insert() Method:
=========================
--> We can Insert a collection of records into the object.
Syntax:
-------
1. Database.Insert(<collectionObjectName>):
Note:
1. TRUE --> Maintain Transaction (Same As INSERT)
2. False --> Allow Partial Processing
Database.SaveResult Class
|
--> Can Store only One Record Result, which has been
inserted.
List<Database.SaveResult>
(OR)
Database.SaveResult[]
Methods:
--------
1. Boolean IsSuccess():
To know the insertion operation result is success or failed.
2. GetID():
It returns of Record ID, once the record has been inserted.
3. Database.Error[] GetErrors():
It returns the Errors information of the failed records.
Database.Error Class:
---------------------
It contains the Errors information of the Failure Records.
Methods:
1. GetMessage():
Returns the Error Message.
2. GetStatusCode():
Returns the Status Code.
Ex:
REQUIRED_FIELD_MISSING
CUSTOM_VALIDATION_FAILED
3. GetFields():
Returns the Affected Fields information.
Ex:
Industry, Rating,...etc.
UseCase:
========
Write an apex program, to insert 100 Hiring Manager Records inside the object
by allowing the Partial Processing.
Display each record processing result in the Debug Log File.
Class Code:
-----------
public class DMLUtility
{
Public static void InsertBulkHRRecords()
{
List<Hiring_Manager__C> lstHRRecords = new List<Hiring_Manager__C>();
if(counter != 95)
{
hrRecord.Email_ID__c = 'testhr'+counter+'@gmail.com';
}
if(counter != 90)
{
hrRecord.Contact_Number__c = '9900887799';
}
if(! lstHRRecords.isEmpty())
{
//insert lstHRRecords;
system.debug('----------------------------------------------');
}
}
}
}
Execution:
----------
DMLUtility.InsertBulkHRRecords();
Delete Statement:
=================
Syntax:
Delete Only One Record:
-----------------------
Delete <objectName>;
UseCase:
========
Write an apex program, to Delete the Records from the Hiring Manager object
whose name is starting with the specified Characters.
Class Code:
-----------
public class DMLUtility
{
Public static void DeleteHRRecords(string startingChars)
{
/*
if(startingChars != null && startingChars != '')
{
List<Hiring_Manager__C> lstHrs = [Select id, name from
Hiring_Manager__C
Where
name like : startingChars+'%'];
if(! lstHrs.isEmpty())
{
// Delete lstHrs;
Database.DeleteResult[] results = Database.Delete(lstHrs, false);
}
}
*/
Execution:
----------
DMLUtility.DeleteHRRecords('TEST HR');
UnDelete Statement:
===================
By using this statement, we can re-store the deleted records back to the
actual object. We can re-store either One / Multiple / All Records back to the
object.
Syntax:
Re-Store Only One Record:
-------------------------
UnDelete <objectName>;
UseCase:
========
Write an apex program, to Re-Store the Hiring Manager Record back to the
object based on the Specified Name.
Class Code:
-----------
public class DMLUtility
{
Public static void RestoreHRRecords(string startingChars)
{
if(startingChars != null && startingChars != '')
{
List<Hiring_Manager__C> hrsToReStore = [Select id, name, isDeleted
from Hiring_Manager__C
Where isDeleted =
true and name like : startingChars+'%'
ALL ROWS];
if(! hrsToReStore.isEmpty())
{
Database.UndeleteResult[] results = Database.UnDelete(hrsToReStore,
false);
}
}
}
}
Execution:
----------
DMLUtility.RestoreHRRecords('TEST HR');
DMLUtility.RestoreHRRecords('TEST HR - 2');
Update Statement:
=================
By using this statement, we can update the records by assigning the new
values for the required fields based on the requirement.
Syntax:
Update Only One Record:
-----------------------
Update <objectName>;
Note:
While updating the records exist in the object, we have to follow the below
steps.
Step 1: Get the Records from the Object, which needs to be get Updated.
Step 2: Assign the New Values for each Record by iterating the Collection.
if(! lstAccounts.isEmpty())
{
for(Account acc : lstAccounts)
{
acc.Rating = 'Hot';
}
// Step 3: Commit the Changes To Database.
Update lstAccounts;
}
UseCase:
========
Write an apex program, to Update the Hiring Managers whose name starting with
"Apex HR" we below.
Location = 'Mumbai'
Contact Number = '8888888888'
Class Code:
-----------
public class DMLUtility
{
Public static void UpdateHRRecords()
{
// Step 1: Get the Required HR Records from the object..
List<Hiring_Manager__c> hrsToUpdate = [Select id, name,
contact_Number__C, location__C
from
Hiring_Manager__C
Where
name like 'Apex HR%'];
Execution:
----------
DMLUtility.UpdateHRRecords();
UseCase:
========
Write an apex program, to DeActivate the User Based on the Specified
UserName.
Class Code:
-----------
public class DMLUtility
{
Public static void DeActivateUser(string uName)
{
if(uName != null && uName != '')
{
User userToDeActivate = [Select id, username, isActive
from User
Where userName =: uName
and isActive = true];
if(userToDeActivate.id != null)
{
userToDeActivate.IsActive = false;
update userToDeActivate;
}
}
}
}
Execute:
--------
DMLUtility.DeActivateUser('manager330@gmail.com');
Upsert Statement:
=================
By using this statement, we can perform both "Update and Insert" into the object at
a time by using a Single DML Statement.
Note:
It will update the records into the Database, which are having the Record ID.
It will insert the records into the object, which are not having the Record Id.
Syntax:
Upsert Only One Record:
-----------------------
Upsert <objectName>;
UseCase:
========
Write an apex program, to Change the Priority as "High" for all New Case
Records. And Create a New Case Record into the object.
Class Code:
-----------
public class DMLUtility
{
Public static void UpsertCaseRecords()
{
// Prepare a Collection..
List<Case> casesToUpsert = new List<Case>();
csRecord.Status = 'New';
csRecord.Priority = 'High';
csRecord.Type = 'Mechanical';
csRecord.Origin = 'Phone';
csRecord.Reason = 'Breakdown';
csRecord.Subject = 'My WebCam is unable to Capture the Videos';
csRecord.Description = 'Dear Customer Support, My Recently
Purchased WebCam is Unable to Capture the Videos. Please have a look into the issue
and resolve asap. Thanks.';
// Add the record to collection..
casesToUpsert.Add(csRecord);
Execution:
----------
DMLUtility.UpsertCaseRecords();
Merge Statement:
================
By using this statement, we can merge the multiple duplicate "Accounts,
Contacts and Leads" into a single record. So that we can avoid the redundancy in
the application.
Syntax:
Merge Two Records:
------------------
Merge <TargetRecord> <SourceRecord>;
Merge 3 Records:
----------------
Merge <TargetRecord> List<SourceRecord>;
List<SourceRecord>, Boolean);
Example:
========
Class Code:
-----------
public class DMLUtility
{
Public static void MergeAccountRecords()
{
Account targetAccount = [Select id, name, rating, industry, annualrevenue,
active__C, phone, fax
from Account
Where name = 'WR
Rule Test Account'
Limit
1];
Execution:
----------
DMLUtility.MergeAccountRecords();