DML in Salesforce

By | January 21, 2020

DML stands for Data Manipulation Language.

Where we want to manipulate the data in the sObject we will use Dml.

Types of DML operations

  1. INSERT
  2. UPDATE
  3. UPSERT
  4. DELETE
  5. UNDELETE
  6. MERGE

Dml can be performed in 2 formats

1. Using  keywords (insert,update,delete,undelete,merge,upsert)(used as for Atomic operation)

2. Using predefined apex class database. (used as for Atomic operation and non atomic operation)

  • Database.insert( )
  • Database.update( )
  • Database.upsert( )
  • Database.delete( )
  • Database.merge( )
  • Database. undelete( )
  • Database.emptyRecycle( )

DML operations are classified into two types

  • Atomic
  • Non-Atomic

1.Atomic: if any one of the record fails during Dml operations entire operation will fail

2.Non atomic : if any one of the record fails during the dml operation ,only that the record will fail and remaining operation will continue to execute.

Example:  Database.insert(sObject[],flag)

flag=true(it is atomic)

flag=flase(it is non atomic)

Note: If lookup (or) masterdetail fields is a standard fields we use id of the record.

Ex:owner:lookup(user):ownerid

Lookup(Account):AccountId

Methods available in DML operations

1.Database.error

This Class will contain all the error that are generated during Dml operation.

Methods:

a) getfields( ): This will return list of fields on which error has occurred.

Syntax:  list<String> getfields( )

b) getMessage( ) :- This method return error message generated during the dml operation.

Syntax: string getMessage( );

c) getStatuscode (): This method return the code that characterize the error.

syntax: statuscode getStatuscode( );

2.Database.Saveresult:

This will contain result generated during DML operation of insert/update.

DMl on every record will return on Database.saveresult.

METHODS

  • ISSUCCESS( ): This method will return true if the DML operation was success for this object,if it fails return fail otherwise.
  • getid( ): Return the Id of the Sobject were you trying to insert or update.
  • geterror( ): This method will return the array of database error occurred if no error occures return an empty set. This will return Database.error[]

3.Database.userResult:

This Class will contain the result of an upsert Dml operation retrived by a database method

METHODS

  • IsCreated( ): This will return true if upsert operation is a insert
  • IsSuccess( ): This method will Return true if the Dml operation was successful for this object if it is fails return false otherwise

Leave a Reply

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