0
Set<Wall_Panel_Type__c> myset = new Set<Wall_Panel_Type__c>();
List<Wall_Panel_Type__c> result = new List<Wall_Panel_Type__c>();
for (Wall_Panel_Type__c s : WallPostConfig) {
    if (myset.add(s)) {
        result.add(s);
    }
}

WallPostConfig contains the list with duplicate entries.

myset recieves the unique list, but result still contains the duplicates. Please help me out.

  • can you please accept the answer and close this question. – Ratan Paul Jun 14 '17 at 5:44
0

try doing this instead:

Set<Wall_Panel_Type__c> myset = new Set<Wall_Panel_Type__c>();
for (Wall_Panel_Type__c s : WallPostConfig) {
    myset.add(s);
}
List<Wall_Panel_Type__c> result = new List<Wall_Panel_Type__c>();
result.addAll(myset);
0

Simple solution using map here

WallPostConfig is a list

//create a map and pass the list in map. This will create unique list 

map<Id, Wall_Panel_Type__c> mapWall_Panel_Type = new map<Id, Wall_Panel_Type__c>(WallPostConfig);

// Now if you want unique list then use mapWall_Panel_Type.values();
0

Assuming you queried the records from the database, they'll each have a unique Id, and therefore will not be considered duplicates. If you want to remove the duplicates, you'll need to first remove the Id values.

Set<Wall_Panel_Type__c> temp = new Set<Wall_Panel_Type__c>(WallPostConfig.deepClone());
List<Wall_Panel_Type__c> result = new List<Wall_Panel_Type__c>(temp);

If you need to keep the Id values, you can use a map:

Map<Wall_Panel_Type__c, Wall_Panel_Type__c> 
  temp = new Map<Wall_Panel_Type__c, Wall_Panel_Type__c>();
for(Wall_Panel_Type__c record: WallPostConfig) {
  Wall_Panel_Type__c cloned = record.clone();
  if(!temp.containsKey(cloned)) {
    temp.put(cloned, record);
  }
}
List<Wall_Panel_Type__c> result = temp.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.