function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
davidjbbdavidjbb 

SOQL + List.

Hello Community. 

 

I have the following List.

 

List<jbbfc2__Fieldworker_Name__c> team = new List <jbbfc2__Fieldworker_Name__c>([Select Team__c From jbbfc2__Fieldworker_Name__c]);
 

 

 I can't do Select Distinct for SOQL, so how can I remove all the duplicate Team__c. I need the values to be inserted into a List collection.

 

I'm not that great in coding, so go easy on me :)

 

Thank you,

Davidjbb

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa

What about... 

List<jbbfc2__Fieldworker_Name__c> team = [Select Team__c From jbbfc2__Fieldworker_Name__c];
Set<String> uniqueTeams = new Set<String>();
for(jbbfc2__Fieldworker_Name__c fn : team){
  if (!uniqueTeams.contains(fn.Team__c)){
    uniqueTeams.add(fn.Team__c);
  }
}

// You can get a List of a set by 
// List<String> yourList = new List<String>();
// yourList.addAll(uniqueTeams);

 Regards

All Answers

SeAlVaSeAlVa

What about... 

List<jbbfc2__Fieldworker_Name__c> team = [Select Team__c From jbbfc2__Fieldworker_Name__c];
Set<String> uniqueTeams = new Set<String>();
for(jbbfc2__Fieldworker_Name__c fn : team){
  if (!uniqueTeams.contains(fn.Team__c)){
    uniqueTeams.add(fn.Team__c);
  }
}

// You can get a List of a set by 
// List<String> yourList = new List<String>();
// yourList.addAll(uniqueTeams);

 Regards

This was selected as the best answer
davidjbbdavidjbb

Thank you for the quick response!

 

if I use

 

// You can get a List of a set by 
// List<String> yourList = new List<String>();
// yourList.addAll(uniqueTeams);

How do I iterate over the list? For example, I need to insert the a.Team, where it's not the duplicate Teams?

 

  for(jbbfc2__Fieldworker_Name__c a : team){
 
options.add(new SelectOption(a.team__c,a.team__c)); 
   
  
 }

 

 edit:

 

I think this worked

 

 for(String teams : uniqueTeams){
options.add(new SelectOption(teams,teams));     
 }

 

Suresh RaghuramSuresh Raghuram

if you would like to get the values with out duplicates

create a set

code will be as follows.

 

set<string> st = new set<string>();

your list

for(jbbfc2_Dieldworker_Name__c  jbb: team){

 

st.add(jbb.Team__c);

}

Here you are in the set you will have all the unique team names. you can use it in any query or some thing else.

I guess this will solve your problem. If you any question keep posting.

If this is answer to your question make this as a solution.

Its up to u.

davidjbbdavidjbb

Thank you for your help everyone!