Skip to main content
Updated Existing code.
Source Link
anxiousAvocado
  • 653
  • 1
  • 16
  • 38

set<string> UserbusinessUnits = new set<string>();

{BusinessAnalyst_Active: {permission_set__c = BusinessAnalyst, business_Unit__c = Active} , BusinessAnalyst_VB :{permission_set__c = BusinessAnalyst, business_Unit__c = VB}, BusinessAnalyst_Retiree :{permission_set__c= BusinessAnalyst, business_Unit__c = Retiree}, Active_BC :{permission_set__c= Active_bc, business_Unit__c = Active}}

    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++;
            }

        }`

set<string> UserbusinessUnits = new set<string>();

{BusinessAnalyst_Active: {permission_set__c = BusinessAnalyst, business_Unit__c = Active} , BusinessAnalyst_VB :{permission_set__c = BusinessAnalyst, business_Unit__c = VB}, BusinessAnalyst_Retiree :{permission_set__c= BusinessAnalyst, business_Unit__c = Retiree}, Active_BC :{permission_set__c= Active_bc, business_Unit__c = Active}}

    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++;
            }

        }`
Source Link
anxiousAvocado
  • 653
  • 1
  • 16
  • 38

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

set<string> UserbusinessUnits = new set<string>();

{BusinessAnalyst_Active: {permission_set__c = BusinessAnalyst, business_Unit__c = Active} , BusinessAnalyst_VB :{permission_set__c = BusinessAnalyst, business_Unit__c = VB}, BusinessAnalyst_Retiree :{permission_set__c= BusinessAnalyst, business_Unit__c = Retiree}, Active_BC :{permission_set__c= Active_bc, business_Unit__c = Active}}

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.