3

I have Trigger,in Trigger I have one select query.That select query returns duplicate records. My query is :

LIST<Company__c> li = LIST<Company__c>();
LIST<String> companynames= LIST<String>(); 
companynames.add('tempabc');
companynames.add('tempabc1');

li= [SELECT Id,Name FROM Company__c WHERE Name IN : companynames];

Note : The Company names come from dynamic list when the Trigger is fired as of now I am hard coded.

But Now I am getting the 3 records from the Company Object. 1).Name=tempabc 2).Name=tempabc 3).Name=tempabc1

I need to query the without duplicate records? Please some one help me.

Best Regards, Ramesh

0

2 Answers 2

2

Why don't use set instead of List this will prevent entering same names.

Set<String> companynames=  new Set<String>(); 
companynames.add('tempabc');
companynames.add('tempabc1');

li= [SELECT Id,Name FROM Company__c WHERE Name IN : companynames];

// This will prevent entering same name records if soql return
Map<String, Company__c> uniqueNameRecords = new Map<String, Comapany__c>();
for(Company__c company : li)
{
   uniqueNameRecords.put(company.name, company); 
}
1
  • Can you explain how this will help or make a difference to "select query returns duplicate records" ? Commented Mar 4, 2014 at 15:24
3

I have a feeling those records are not duplicates, but rather different records with the same name. I would output the Ids and verify against them.

4
  • I was going to answer the same, there is no way soql would return duplicates. Commented Mar 4, 2014 at 15:23
  • You are absolutely correct Mr Jessen Altman.Id are different but Names are same. Commented Mar 4, 2014 at 15:29
  • @Ramesh glad I could help Commented Mar 4, 2014 at 15:32
  • I would also recommend using a Set<Id> rather than a List. When you add these Ids to your set, please remember to check their sizes in case you are providing a mix of 18 and 15 character Ids.
    – Double A
    Commented Mar 4, 2014 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .