You need to sign in to do that
Don't have an account?
F Smoak
Trying to populate Map<Id,Set<String>> but cannot crack how to ensure no duplicate values of string are put in map.
Hi
I am writing a trigger wherein as first step I need to create a map containing its parent id, Set of text field values which are possibly duplicates for that object records.
Code:
trigger CMS_Insert_MCCP on CMS_Rule_Line_Item__c (after Insert) {
Map<id,Set<String>> poateamMap = new Map<id,Set<String>>(); //poaid vs set of team
List<CMS_Rule_Line_Item__c> cbList= trigger.new;
for(CMS_Rule_Line_Item__c crRecord: cbList){
if(!poateamMap.containsKey(crRecord.Rule_Name__c)){
poateamMap.put(crRecord.Rule_Name__c,new Set<String>{crRecord.CMS_Team__c});
}
else{
Set<String> str = new Set<String>();
str = poateamMap.get(crRecord.Rule_Name__c);
if(str != crRecord.CMS_Team__c) {
poateamMap.put(crRecord.Rule_Name__c,crRecord.CMS_Team__c);
}
} }
Here Iwould like to ensure that Rule_Name__c (stores parent id) has corresponding set of records having only one distinct team value for it in that set.
I am missing something but cannot figure out. Please help!
I am writing a trigger wherein as first step I need to create a map containing its parent id, Set of text field values which are possibly duplicates for that object records.
Code:
trigger CMS_Insert_MCCP on CMS_Rule_Line_Item__c (after Insert) {
Map<id,Set<String>> poateamMap = new Map<id,Set<String>>(); //poaid vs set of team
List<CMS_Rule_Line_Item__c> cbList= trigger.new;
for(CMS_Rule_Line_Item__c crRecord: cbList){
if(!poateamMap.containsKey(crRecord.Rule_Name__c)){
poateamMap.put(crRecord.Rule_Name__c,new Set<String>{crRecord.CMS_Team__c});
}
else{
Set<String> str = new Set<String>();
str = poateamMap.get(crRecord.Rule_Name__c);
if(str != crRecord.CMS_Team__c) {
poateamMap.put(crRecord.Rule_Name__c,crRecord.CMS_Team__c);
}
} }
Here Iwould like to ensure that Rule_Name__c (stores parent id) has corresponding set of records having only one distinct team value for it in that set.
I am missing something but cannot figure out. Please help!
By default set will not allow duplicate:
You can choose any one of the below code.
If you want do more check then try the below code:
Thanks,
Maharajan.C
In this set of strings which are values in map it is normally a scenario where one Rule_Name__c has say t1,t2,t3 teams and next rule name has t2,t3,t4 teams so I need ensure to have a set which consists t1,t2,t3,t4:
I have written this as :
Set<String> teamSet = new Set<String>{poateamMap.getvalues()};
But looks like I have some syntax error, can you please let me know what am I missing?
Please try the below way:
Thanks,
Maharajan.C
Then in that case I can utilise above code to populate teamSet like below, will this work same way?:
trigger CMS_Insert_MCCP on CMS_Rule_Line_Item__c (after Insert) {
Map<id,Set<String>> poateamMap = new Map<id,Set<String>>(); //poaid vs set of team
List<CMS_Rule_Line_Item__c> cbList= trigger.new;
Set<String> teamSet = new Set<String>();
for(CMS_Rule_Line_Item__c crRecord: cbList){
if(!poateamMap.containsKey(crRecord.Rule_Name__c)){
poateamMap.put(crRecord.Rule_Name__c,new Set<String>{crRecord.CMS_Team__c});
teamSet.add(crRecord.CMS_Team__c);
}
else{
poateamMap.get(crRecord.Rule_Name__c).add(crRecord.CMS_Team__c);
teamSet.add(crRecord.CMS_Team__c);
} } }