Salesforce Free Developer >

Chapter 6 - Apex DML and Database Methods >

Delete | Apex DML Standalone Statement

Delete | Apex DML Standalone Statement

Ex: Delete Duplicate Contacts

List<Contact> conList = [select firstname, lastname from contact];

Map<String, ID> mMap = new Map<String, ID>();

for(Contact c: conList)

{

mMap.put(c.name, c.id);

}

List<Contact> uniqList = new List<Contact>();

List<Contact> delList = new List<Contact>();

Set<String> sSet = mMap.keySet();

Set<ID> uniqSet = new Set<ID>;

for(String s: sSet)

{

uniqSet.add(mMap.get(s));

}

for(Contact c1: conList)

{

if((uniqSet.contains(c1.id)))

uniqList.add(c1);

else

delList.add(c1);

}

delete delList;

Note: Use isDeleted=true & ALL ROWS keyword to get the deleted records from the recycle bin using SOQL.

Note: ALL ROWS keyword cannot be used in the query editor but can be used in the apex class or function while querying record in [].

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners. But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

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