Saturday, September 23, 2023
HomeUncategorizedSalesforce | DML Operations in Apex: All You Must Know

Salesforce | DML Operations in Apex: All You Must Know


On this Weblog, we’ll research DML Operations in Apex.

  • DML is used to insert, replace, delete and undelete data. 
  • We are able to use upsert to both insert or replace a report. 
  • We additionally use merge when duplicate leads, contacts and accounts are current and merge them into one report, others are deleted and associated data are reparented. 
  • We should always all the time carry out DML operations in bulk. 
  • We are able to deal with exceptions. 

Insert Data 

  • Instance of Inserting Solely One File 
Account acc = new Account(Title=’Tech College’, Telephone=’123456’); 
insert acc;
  • Fetch the Id of the Inserted File 
System.debug(‘Acc ID = >‘ +;
  • Inserting Data in Bulk utilizing Listing 
Listing<Account> accList = new Listing<Account>(); 
Account acc = new Account(Title=’record1’ , Telephone=’12345’); 
Account acc1=new Account(Title=’record2’ , Telephone=’12543'); 
Insert accList;
  • Governor Restrict Test
    • Inserting two data individually then two DML will likely be counted. 
    • But when we insert two data by an inventory then one DML will likely be counted. 
  • Inserting Associated Data, for instance first insert account after which contact
Account acc = new Account(Title=’record1’ , Telephone=’12345’); 
insert acc; 
Contact con = new Contact(Title=’abc’ , LastName=’xyz’ ,; 
insert con;

dont miss out iconRemember to take a look at: Best Practices to Avoid Excessive SOAP and REST API DML | Salesforce Developer Guide

Replace Data 

  • Question an Current File and Print its Values
Account acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
  • Then, Replace the File and do Replace DML
replace acc;
  • Now, Question the Up to date File
Account updatedAcc=[Select Id,Name,Phone FROM Account Where Name=’record1’];
  • Now apply System.assertEqual to Validate Replace 
Contact con = [Choose ID,FirstName,LastName,Telephone,Account.cellphone From Contact the place FirstName=’abc’ AND LastName=’xyz’ and AccountId!=null); 
replace con; 
replace con.Account;

So, on this approach firstly contact report will likely be up to date, and after that its associated account may also be up to date. 

dont miss out iconTry one other wonderful weblog by Bhawana right here: Learn All About Collections in Salesforce: List, Set and Map

Upsert Data 

This operation permits us to Create one report and replace the present one utilizing upsert DML. 

Right here we are able to use an inventory to upsert data. 

Listing<Account> accList=new Listing<Account>(); 
Account acc1= new Account(Title=’record1’,Telephone=’12765’); 
Account acc2=[Select ID,Name,Phone FROM Account WHERE Name=’record2’]; 
Upsert accList;

Merge Data 

  • So solely lead, Contact and Account data might be merged. 
  • We are able to solely merge as much as three data of the identical sObject sort into one. 
  • For instance, if three leads have the identical worth then we are able to merge them into one similar goes for contact and account data. 
  • After merging the data, the outdated report will likely be deleted and merged into a brand new one. 

So, report 1 has associated contact and report 2 doesn’t have any associated contact then if we merge them then we are able to have associated contact in report 2 as properly. 

Account mergeinto=[Select ID,Name,Phone FROM Account WHERE Name=’record2’]; 
Account mergefrom=[Select ID,Name,Phone FROM Account WHERE Name=’record1’]; 
Merge mergeinto mergefrom;

Delete Data 

  • Apply SOQL. 
  • Now delete queried data utilizing delete DML. 
Account acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
delete acc; 
And to delete a couple of report you'll be able to create record. 
Listing<Account> acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
delete acc;

Undelete Data 

  • Insert a report. 
  • Delete that report. 
  • Now question deleted data utilizing all rows within the question. 

All rows will question all of the data whether or not they’re deleted or undeleted. 

  • Then apply undelete assertion. 
Account deletedacc= [Select Id,Name,Phone FROM Account Where Name=’record1’ ALL ROWS]; 
undelete deletedacc;

DML Assertion Exception 

Apply to try to catch to deal with exceptions raised when DML operations fail. 

Account acc = new Account(); 
insert acc; 
catch(DMLException e){ 
System.debug(‘Error=>' + e.getMessage()); 

So, it will present all of the DML errors which can seem whereas performing DML operations contained in the org. Resembling right here, on this case, we are able to see that the account report is created and not using a identify so it is going to present an error. 


Source link



Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments