DML Operations

1. What are DML operations?
A: DML Operations:  
Insert ,  Update, Upsert, delete, Undelete, Merge, rollback, savepoint, emptyRecylebin.

2. What are Atomic Operations?

A: Atomic Operation:
Which means if any one of the operation fails entire operation will fail.
  Ex: List<Account> accs=new List<Account>();
Account a1=new Account(Name='aaa');
Account a2=new Account();
accs.add(a1);
accs.add(a2);
insert accs;
o/p :Entire opeation of insert will fail.

3. What are non-Atomic operations?

A: Which means if any one of the operation fails remaining operation will run i.e., if error occurs the remaining records will be inserted / updated means partial DML operation will be done.
Ex:  List<Account> accs=new List<Account>();
        Account a1=new Account(Name='aaa');
Account a2=new Account();
accs.add(a1);
accs.add(a2);
Database.insert accs;
 // In the above scenario operation is non-Atomic operation

4. What is difference between insert and Database.insert?

A: Insert and Database.insert: 
    insert is the DML statement which is same as databse.insert. However, database.insert gives           more flexibility like rollback, default assignment rules etc.

        If we use the DML statement (insert), then in bulk operation if error occurs, the execution will stop and Apex code throws an error which can be handled in try catch block.

    If DML database methods (Database.insert) used, then if error occurs the remaining records will be inserted / updated means partial DML operation will be done.
Ex: List<Account> accs=new List<Account>();
        Account a1=new Account(Name='aaa');
Account a2=new Account();
accs.add(a1);
accs.add(a2);
         insert accs;
Database.insert accs;

5. How to empty the recycle bin?

A: Database.emptyRecycleBin. This method is used for Deleting the records from RecycleBin.
You may empty your recyclebin by using emptyRecycleBin method of Database class. These are the variant of the method available,
Database.emptyRecycleBin(ID[])    Pass a list of Ids of the records that you want to permanently delete from recyclebin.
Database.emptyRecycleBin(sObject[])    Pass a list of sObjects of the records that you want to permanently delete from recyclebin.
Database.emptyRecycleBin(sObject)    Pass the sObject instance of the record that you want to permanently delete from recyclebin.

6. How to hard delete the data?

A: By using Database.emptyRecycleBin.

7. How many DML statements we can write in a single transaction?

A: With in a single transaction we can only write 150 Dml statements.

8. What is Upsert Operation?

A: This keyword is used to creates/insert new records and  updates existing records. See the below example to understand how can we use this in apex code.
Example:
            Account[] acctsList = [SELECT Id, Name, BillingCity FROM Account WHERE BillingCity               = ‘Bombay’];
          for (Account a : acctsList){
                     a.BillingCity = ‘Mumbai’;
         }
         Account newAcct = new Account(Name = ‘Ramco’, BillingCity = ‘Hyderabad’);
         acctsList.add(newAcct);
        try {
              upsert acctsList;
       }
       catch (DmlException e) {
       } 

9. What is the user external Id in the Upsert opeartion?

A:You can use the SObject Rows by External ID resource to create new records or update existing records (upsert) based on the value of a specified external ID field.
 External Id available for Text, Number and Email field types.
Eternal Id is used in upsert operations in Data Loader.

  • If external id is notmatched then a new record is created.
  • If external id matched once then record will be updated.
  • If external id is matched multiple times then error occurs.

10. Which fields can be External Id fields?
A:  External Id available for Text, Number and Email field types.

11. How to undelete the data using DML?

A: This is used to undelete the reocord which are in recyclebin
 Syntax : Undelete sobject |Sobject[]
 Example : List<Account> accs=[select id from Account where isDeleted=true ALL ROWS];                            
undelete accs;

12. What is merge Statement in DML?

A: Merge: This keyword merges up to three records of the same type   into one of the records, deleting the others, and re-parenting any related records.
Example:
List<Account> accList = new List<Account>{new Account(Name=’Myacc1′),new                                                                       Account(Name=’MyAcc2′)};
insert accList;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = ‘Myacc1’ LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = ‘MyAcc2’ LIMIT 1];
try {
merge masterAcct mergeAcct;
}
catch (DmlException e) {
}

13. When we perform merge operation what will happen to the child records of the record which is deleted?

A: When you have duplicate lead, contact, or account records in the database, cleaning up your data and consolidating the records might be a good idea. You can merge up to three records of the same sObject type. The merge operation merges up to three records into one of the records, deletes the others, and reparents any related records.



Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Reach to the best Data Science Training institute in Chennai for skyrocketing your career, Infycle Technologies. It is the best Software Training & Placement institute in and around Chennai, that also gives the best placement training for personality tests, interview preparation, and mock interviews for leveling up the candidate's grades to a professional level.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Page Layouts

SOQL Scenario-1