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

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); 
}
  • Can you explain how this will help or make a difference to "select query returns duplicate records" ? – Samuel De Rycke Mar 4 '14 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.

  • I was going to answer the same, there is no way soql would return duplicates. – Samuel De Rycke Mar 4 '14 at 15:23
  • You are absolutely correct Mr Jessen Altman.Id are different but Names are same. – Ramesh S Mar 4 '14 at 15:29
  • @Ramesh glad I could help – Jesse Altman Mar 4 '14 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 Mar 4 '14 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.