+ Start a Discussion
anu.s1.3587621171560732E12anu.s1.3587621171560732E12 

How to delete lead duplicate records using do while loop

Hi,

 

I just uploaded 3000 more lead records in my salesforce instance. After insersion I find out the duplicate lead records with same phone number.now i want to delete the duplicate records based on phone number and also Industry criteria.

is it possible to do using do while loop in the trigger.if it is  possible means please send me an example code.

Best Answer chosen by Admin (Salesforce Developers) 
SharathChandraSharathChandra

we can do by do while but i prefer for loop over here

 

List<String> myphoneList = new List<String>();

for(Lead l: [ SELECT Name,Phone FROM Lead]){

myphoneList.add(l.Phone);

}

List<Lead> mydups = new List<Lead>();

for(Lead l:[SELECT Name,Phone FROM Lead WHERE Phone IN :myphoneList]){

//add duplicate into one list

mydups.add(l);

}

//delete

delete mydups;

All Answers

Rakesh KumarRakesh Kumar

 

Istead of using do while loop you can easy to get duplicate records of loop by using

List.contains() method.

 

 

anu.s1.3587621171560732E12anu.s1.3587621171560732E12

Actuallly my problem is

suppose if one record dont have criteria the other one can have criteria,but phone numbers are same for both the records.

so I want to merge the criteria and I want to delete remaining records.

 

It could be thankful if you send any example code regading this issue.

SharathChandraSharathChandra

we can do by do while but i prefer for loop over here

 

List<String> myphoneList = new List<String>();

for(Lead l: [ SELECT Name,Phone FROM Lead]){

myphoneList.add(l.Phone);

}

List<Lead> mydups = new List<Lead>();

for(Lead l:[SELECT Name,Phone FROM Lead WHERE Phone IN :myphoneList]){

//add duplicate into one list

mydups.add(l);

}

//delete

delete mydups;

This was selected as the best answer
SharathChandraSharathChandra

@ Rakesh

We dont have contains method for list in salesforce

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm

anu.s1.3587621171560732E12anu.s1.3587621171560732E12

Thank you so much for the code.