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]){
            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);


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.

  • 2
    What Apex have you tried so far? It looks like you just need to loop over your Custom Settings records and accumulate a Set, like with any other sObjects. If you'll share more code, we can provide more tailored suggestions. – David Reed Mar 25 '19 at 16:53
  • I just updated the code I have, I am running the loop and was checking if it exist. Now the problem is I have 1 permission set name for which I have 5 records so the Name field has to be unique so I added prefixes there like _vb, _Active. I am trying to get all of those and their respective BusinessUnit value. – apple123 Mar 25 '19 at 16:58

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.

 system.debug('220 --- All Map Keys :'+mapKeys);

 for(string s : MapKeys){
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.