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
F SmoakF 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!
Maharajan CMaharajan C
Hi Smoak.

By default set will not allow duplicate:

You can choose any one of the below 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{
			   poateamMap.get(crRecord.Rule_Name__c).add(crRecord.CMS_Team__c);
            } 
		}
}

If you want do more check then try the below 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.contains(crRecord.CMS_Team__c)) {
                   poateamMap.get(crRecord.Rule_Name__c).add(crRecord.CMS_Team__c);
				}
            } 
		}
}

Thanks,
Maharajan.C
F SmoakF Smoak
Thanks Maharajan!

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?
 
Maharajan CMaharajan C
Hi Smoak,

Please try the below way:
 
Set<String> teamSet = new Set<String>();
for(String str : poateamMap.keyset()){
   for(String s : poateamMap.get(str)){
		 system.debug(' --- ' + s );
		 teamSet.add(s);
	}
}

Thanks,
Maharajan.C
F SmoakF Smoak
Thanks, I was hoping to get something in one line.
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);
} } }