[SalesForce] How to check in a map if specific value exist under multiple keys in Apex Map

I have a Map of Permission set and the business Unit like below, the map is being generated from a custom setting

    Map<string, Allow_disposition__c> adobj = new Map<string, allow_Disposition__c>();

    for(Allow_Disposition__c  vaObj: Allow_Disposition__c.getall().values()){
            adobj.put(vaobj.Name, vaObj);
    }

    system.debug('198 -- Map : '+adobj);

    String profileName = [SELECT Name FROM Profile WHERE Id =: UserInfo.getProfileId()].name;
    system.debug('User Profile Name :'+ProfileName);

    // if user has system admin skip the logic and enable permission, else run the logic and give appropriate permission

    if(profileName != 'System Administrator'){
        //Not system admin, run the logic
        for(PermissionSetAssignment PSAobj : [SELECT Id, PermissionSetId, PermissionSet.Name, AssigneeId 
                                                FROM PermissionSetAssignment WHERE AssigneeId =:UserInfo.getuserId()
                                                AND PermissionSet.isOwnedByProfile = false]){
            psets.add(PSAObj.PermissionSet.Name);
            system.debug('214 --- Psets'+psets);

            for(integer i=0;i<=adObj.size(); i++){
                if(adobj.containsKey(PSAObj.PermissionSet.Name)){ //this won't work now because the **Key** does not contain Business Analyst, Rather it contains Business Analyst_VB and busienss_Analyst_Active and so forth.
                    system.debug('218 -- Permission Set Bs :'+adobj.get(PSAObj.PermissionSet.Name).business_unit__c);
                        userbusinessUnits.add(adobj.get(PSAObj.PermissionSet.Name).business_unit__c);
                }
                 i++;
            }

        }`

How do I retrieve all values of Business_Unit__c for the field permission_set__c business Analyst from the value and add it to the set above? I am not able to add that permission_set__c field as key because Map will only save one unique key so in custom settings I have added data as above. I want to retrieve All business Unit values for businessAnalyst permission set, and I am getting BusinessAnalyst name from the SOQL on permission set assignment object which also matches the value in permission_set__c field.

Best Answer

So, I was able to solve it with one assumptions that the Key of the Map (Name field of the custom settings) will always contain the sub-string from permission_set_Name.

I add the Keys of the Map to a set of String and from their I run a loop on set to check if the permission set sub string exist in the set of strings. If it does then I add the business_unit field from the Values of Map to a new set.

 if(adObj!=null){ 
     mapKeys.addAll(adObj.keyset());  
 }
 system.debug('220 --- All Map Keys :'+mapKeys);

 for(string s : MapKeys){
     if(s.contains(PSAObj.PermissionSet.Name)){
                 userbusinessUnits.add(adobj.get(s).business_unit__c);
                }                                            
            }
Related Topic