You need to sign in to do that
Don't have an account?

filtering a list with duplicate values
I have a method that returns a Map<Id, List<Id>>
Here is the method:
private Map<Id, List<Id>> getDbrToAccountMap(Set<Id> dbrIds) { Map<Id, List<Id>> dbrAccountMap = new Map<Id, List<Id>>(); List<Id> accountIds = new List<Id>(); for(DBR_Group_Member__c member : [select Id, Contact__c, Contact__r.AccountId, DBR__c from DBR_Group_Member__c where DBR__c in: dbrIds]) { if(accountIds.isEmpty()) { accountIds = new List<Id>(); dbrAccountMap.put(member.DBR__c, accountIds); } accountIds.add(member.Contact__r.AccountId); } return dbrAccountMap; }
I only want to add to the list of accountIds for a unique DBR_c and unique account of the Contact_c
For example, if I have a DBR number of:
3333 and the contact is John Smith that is on the Acme account (add to the list)
next I have
3333 and the contact is Jane Smith that is on the Acme account (don't add to the list)
next I have
3333 and the contact is Bob Smith that is on the Big Company account (add to the list)
next I have
3333 and the contact is Doug Smith that is on the Big Company account (don't add to the list)
The DBR to account should be distinct.
In my method, I'm returning the accountIds, but they are not distinct based on the DBR. I have to be careful because I don't want to remove a duplicate accountId in the list if the DBR number was different. That would be a valid element in the list.
I was thinking instead of returning a Map<Id, List<Id>>
I could return a Map<Id, Map<Id, List<Id>>> where the key to the outer map is the DBR__c and the value in the inner map. The key to the inner map would be the contact and the value would be the list of account ids. I was trying that, but couldn't get it to work correctly.
Any help would be appreciated.
Thanks.
Hi,
Use a set and add this unique you need as a string to the set.
Something like this: