0

I have a map from which I want to place the Id's of a field into a List. I tried the following:

Map<String, Relatie__c> uniqueHuish = new map<String, Relatie__c>(); 
    for(Relatie__c rc: rcList) {
        if(!uniqueHuish.containsKey(rc.Huishouden__c)) {
            uniqueHuish.put(rc.Huishouden__c,rc);
        }
    }
    Relatie__c[] uniqueHuishList = uniqueHuish.values();

If I debug uniqueHuishList I get:

DEBUG|uniqueHuishList (Relatie__c:{Contactpersoon__c=0014E00000PD14ABCD, Account__c=0032E00000Vag7ABCD, Huishouden__c=a133E000000HyABCD, Relatietype__c=Lid, Id=a009E000002YaCABCD})

What I want to end up with is a List of only the IDs of Huishouden__c. How do I extract the Huishouden__c into a list? If i try keyset() I get the error Illegal assignment from set to list.

1
  • are you looking some thing like this:map<id,list<id>>uniqueHuish=new map<id,list<id>>(); for(Relatie__c rc: rcList) { if(!uniqueHuish.containsKey(rc.Huishouden__c)) { uniqueHuish.put(rc.id,new List<id>()); } else{ uniqueHuish.get(rc.id).add(rc.Huishouden__c); }if you put system.debug(':::'+uniqueHuish.values()); //List of ids of Huishouden__c May 24 '18 at 6:12
1

You can use a Set to populate the list of field values from your SObject list.

Set<String> uniqueHuish = new Set<String>(); 
for(Relatie__c rc: rcList) {
        uniqueHuish.add(rc.Huishouden__c);
    }
}

of if you want to continue using the map if its being used for some other purpose, then you can retrieve the set of Ids (Keys) alone using

uniqueHuish.keySet()
5
  • @vamshi krishna your code: "Relatie__c" --> Set<String,Relatie__c> uniqueHuish = new Set<String, Relatie__c>(); May 24 '18 at 6:17
  • thx @AnnappaPH. updated. May 24 '18 at 6:30
  • welcome @Vamshi Krishna :) May 24 '18 at 6:32
  • @VamsiKrishna Can I use ID instead of string in this case?
    – Thomas
    May 24 '18 at 6:34
  • yes @Thomas you can use Set<Id> .. May 24 '18 at 6:36
1

You can add all elements of set to a list of same type.

Map<String, Relatie__c> uniqueHuish = new Map<String, Relatie__c>(); 
for(Relatie__c rc: rcList) {
    if(!uniqueHuish.containsKey(rc.Huishouden__c)) {
        uniqueHuish.put(rc.Huishouden__c, rc);
    }
}
List<ID> uniqueHuishList = new List<ID>();
uniqueHuishList.addAll(uniqueHuish.keySet());
system.debug(':::'+uniqueHuishList);
2
  • I get an incorrect signature: void addAll(Set<String>) from the type List<Id>?
    – Thomas
    May 24 '18 at 6:33
  • Typo. List uniqueHuishList must be declared as List<String> May 24 '18 at 7:54
0

If you are looking for map try this code,or using set or list set you can easily collect the ids.

 map<id,list<id>>uniqueHuish=new map<id,list<id>>(); 
  for(Relatie__c rc: rcList) { 
  if(!uniqueHuish.containsKey(rc.Huishouden__c)) { 
   uniqueHuish.put(rc.id,new List<id>()); 
  } 
   else
    { 
    uniqueHuish.get(rc.id).add(rc.Huishouden__c); 
 }
system.debug(':::'+uniqueHuish.values()); 

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.