+ Start a Discussion
kkr.devkkr.dev 

remove the duplicates from list

Hi All,

 

 How to remove the duplicate records from list.Any Idea?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
kkr.devkkr.dev

Thanks Srikanth . I got the solution based on your suggestion.

 

public list<Opportunity> getOpportunities(){
   opptyList = [Select o.StageName, o.Name,o.Id,o.ownerId,o.owner.name,o.CloseDate,o.of_Days_Open__c,o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Product_Specialist__r.Id =: UserInfo.getUserId()];
    
    otmlist=[Select o.UserId,o.OpportunityId, o.Id From OpportunityTeamMember o where o.UserId=:UserInfo.getUserId()];
    
   for(OpportunityTeamMember ot: otmlist){
        oId.add(ot.OpportunityId);
    }
    
   opptyList2 = [Select o.StageName, o.Name, o.Id,o.ownerId,o.owner.name, o.CloseDate,o.of_Days_Open__c, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Id in:oId];
    
   opptylist.addAll(opptyList2);
    
   for(Opportunity o:opptylist){
        oppSetId.add(o.Id);
    }
    
    opptyList3 = [Select o.StageName, o.Name, o.Id,o.ownerId,o.owner.name,o.CloseDate,o.of_Days_Open__c,o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.Id in:oppSetId];
    
    return opptyList3;
   }
}

All Answers

Starz26Starz26

Put them in a map

 

or if you just need IDs, put the iDs in a set

 

Map<ID, Account> will get you one account per ID

 

If you must have a list, then loop throught the map and add the records to the list one at a time.

 

In order to .addAll the map would have to be I believe Map<ID, Account[]> then you could .addAll(mapName.values());

skodisanaskodisana

Hi,

 

Add all the list elements to the Set using allAdd method.
Set will not allow duplciate values.

 

Thanks,

Srikanth. K

Starz26Starz26

doh, I keep forgetting you can put sObjects in sets....good call.

kkr.devkkr.dev

Thanks Srikanth and Starz26. Inspite of  using addall() i am getting the duplicates in the set.Any idea?

skodisanaskodisana

Hi,

 

Set will not allow duplicate values could you please debug and check....

Thanks,

Srikanth. K

kkr.devkkr.dev

Thanks Srikanth.Below is my  getter in my controller. I still see duplicates in the set.Can you pls help me

 

public list<Opportunity> getOpportunities(){
       opptyList = [Select o.StageName, o.Name, o.Id, o.CloseDate, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName =   'Open' and o.Product_Specialist__r.Id =: UserInfo.getUserId()];
   

otmlist=[Select o.UserId,o.OpportunityId, o.Id From OpportunityTeamMember o where o.UserId=:UserInfo.getUserId()];
   

for(OpportunityTeamMember ot: otmlist){
        oId.add(ot.OpportunityId);
    }
   opptyList2 = [Select o.StageName, o.Name, o.Id, o.CloseDate, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Id in:oId];
    
    opptylist.addAll(opptyList2);
    oppSet.addAll(opptyList);

    system.debug('opp set '+ oppSet);
    opptyList3.addAll(oppSet);
   

   return opptyList3;
   }

skodisanaskodisana

HI,

 

Set checks duplcaite logic on entire field values that are queried.

add the Opportunity id to the set and re query with Set of ids.

 

Thanks,

Srikanth. K

kkr.devkkr.dev

Sorry Srikanth.I didn't get you.Could you pls explain me?

 

Thanks

kkr.devkkr.dev

Srikanth,

 

  Could you pls explain this 'add the Opportunity id to the set and re query with Set of ids.'

 

Thanks

Jerun JoseJerun Jose

Hi,

 

Use the code below.

 

Client_Group__c public list<Opportunity> getOpportunities(){
	opptyList = [Select o.StageName, o.Name, o.Id, o.CloseDate, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName =   'Open' and o.Product_Specialist__r.Id =: UserInfo.getUserId()];
	otmlist=[Select o.UserId,o.OpportunityId, o.Id From OpportunityTeamMember o where o.UserId=:UserInfo.getUserId()];
	for( Opportunity opp : opptyList)
	{
		oId.add(opp.ID);
	}
	for(OpportunityTeamMember ot: otmlist){
        oId.add(ot.OpportunityId);
    }
	opptyList2 = [Select o.StageName, o.Name, o.Id, o.CloseDate, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Id in:oId];

	return opptyList2;
}

 

kkr.devkkr.dev

Thanks Srikanth . I got the solution based on your suggestion.

 

public list<Opportunity> getOpportunities(){
   opptyList = [Select o.StageName, o.Name,o.Id,o.ownerId,o.owner.name,o.CloseDate,o.of_Days_Open__c,o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Product_Specialist__r.Id =: UserInfo.getUserId()];
    
    otmlist=[Select o.UserId,o.OpportunityId, o.Id From OpportunityTeamMember o where o.UserId=:UserInfo.getUserId()];
    
   for(OpportunityTeamMember ot: otmlist){
        oId.add(ot.OpportunityId);
    }
    
   opptyList2 = [Select o.StageName, o.Name, o.Id,o.ownerId,o.owner.name, o.CloseDate,o.of_Days_Open__c, o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.StageName = 'Open' and o.Id in:oId];
    
   opptylist.addAll(opptyList2);
    
   for(Opportunity o:opptylist){
        oppSetId.add(o.Id);
    }
    
    opptyList3 = [Select o.StageName, o.Name, o.Id,o.ownerId,o.owner.name,o.CloseDate,o.of_Days_Open__c,o.Amount, o.Employee__c,o.Employee__r.Name,o.Employee__r.Id From Opportunity o where o.Id in:oppSetId];
    
    return opptyList3;
   }
}

This was selected as the best answer