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
KRITI LAHA 8KRITI LAHA 8 

How to get other fields from below code

Hello,

I have beow code as per my requirement. Usecase is like that: 
I have a list of permission sets (PS1-10) and one profile. Below are the steps which I need to execute:
First I need to find the users who have this particular profile and PS7. 
Then I need to check if the users having PS7 and this profile have all the permission sets (PS1-10) assigned to them. 
If there are less permission sets assigned, then return Missing, if the user has PS7 and any other permission set assigned to him, apart from PS1-10, then retuen Additional, else return No Change. 
The final output should give me the userID, the permission sets assigned to them and Missing/Additional/No change. and also all the related fields of a user like firstname, last name etc..
Here I am sharing my code which I am using, it is giving me Id, status(Missing/additional), and permission list but I am facing error when I am adding other fields, As of now, I have tried with 'Division' field but it's throwing error : (Variable does not exist: division)

Can anyone please helpp me on this? I am shaing the code which I am using. Thanks

public class PersonaReport {

    private static String profileName = 'HSE - Support';
    private static String ps7Name = 'HSE - Omni Supervisor';
    
    private static List<String> ps1_ps10NameList = new List<String>{
        'Health Cloud Permission Set License','Multi-Factor Authentication Required','HSE - Omni Supervisor','Access Walkthroughs','myTrailhead Access','HSELIVE - Supervisor'};
    
    
    public static List<UserDetail> getUserPermissionDetails(){

        Map<Id, User> userMap = new Map<Id, User>([SELECT Id
                                  FROM User
                                  WHERE Profile.Name = :profileName and Id IN (SELECT AssigneeId 
                                                   FROM PermissionSetAssignment
                                                   WHERE PermissionSet.label = :ps7Name and PermissionSet.IsOwnedByProfile =false)]);
        //System.debug(usermap);
     Map<Id, List<String>> permissionSetMap = new Map<Id, List<String>>();
        system.debug('permissionSetMapTest'+permissionSetMap);
        
        for(PermissionSetAssignment psa : [SELECT AssigneeId, Assignee.FirstName, Assignee.LastName, Assignee.profile.name, Assignee.Username, Assignee.Email, Assignee.Division, Assignee.Department, PermissionSet.label
                                              FROM PermissionSetAssignment
                                              WHERE AssigneeId = : userMap.keySet()
                                                  AND (PermissionSet.label IN :ps1_ps10NameList
                                                     OR PermissionSet.label NOT IN :ps1_ps10NameList)and PermissionSet.IsOwnedByProfile =false
                                              ORDER BY AssigneeId, PermissionSet.label])
        {
            List<String> tempList = new List<String>();
               if(permissionSetMap.containsKey(psa.AssigneeId))
            tempList = permissionSetMap.get(psa.AssigneeId);
            
               tempList.add(psa.PermissionSet.label);
            tempList.add(psa.Assignee.division);
            permissionSetMap.put(psa.AssigneeId, tempList);
            system.debug('tempdivision'+permissionSetMap );
           
                    
        }

        return evaluateUser(permissionSetMap);
        
    }
    
    
    private static List<UserDetail> evaluateUser(Map<Id, List<String>> permissionSetMap){

        Boolean additional;

        List<UserDetail> userDetailList = new List<UserDetail>();

        List<String> foundList = new List<String>();        
        for(Id id : permissionSetMap.keySet())
        {
            UserDetail ud = new UserDetail();
            additional = false;
            foundList.clear();

            for(String str : permissionSetMap.get(id)){
            
                if(!ps1_ps10NameList.contains(str)){
                    additional = true;
                }else{
                    foundList.add(str);
                }
                /*system.debug(userDetailList);*/
            }            
          system.debug('permissionsetmaptest'+permissionSetMap);
            ud.Id = id;
            ud.permissionSetList = permissionSetMap.get(id);
            
           Ud.Division=permissionSetMap.get(id).division;
           
           system.debug('udinsideEU'+ ud);
            
            if(additional && ps1_ps10NameList.size() == foundList.size())
                ud.status = 'No Change And Additional';
            else if(additional && ps1_ps10NameList.size() != permissionSetMap.get(id).size())
                ud.status = 'Missing And Additional';
            else if(!additional && ps1_ps10NameList.size() != foundList.size())
                ud.status = 'Missing';
            else if(!additional && ps1_ps10NameList.size() == foundList.size())          
                ud.status = 'No Change';
                
            userDetailList.add(ud);
        }
       
        System.debug('evaluateUd details'+userDetailList);
        return userDetailList;
       
        
    }
public class UserDetail {
        
        public Id id;
        public List<String> permissionSetList;
        public String status;
        public String FirstName;
        public String LastName;
        public String Username;
        public String Email;
        public String Division;
        public String Department;
        
    } 
    
    public static void createcsv(){
      List<UserDetail> theListReturnedFromMethod2 =getUserPermissionDetails(); 
        system.debug('inside createcsv'+theListReturnedFromMethod2);
        List<String> header= new List<String> {'AssigneeId','First Name','Last Name','Profile Name','User Name','Role Name','Email','Division','Department','Comapany Name','Last Login Date','Last Modified By','Created Date', 'PS Status' , 'Permissionset Name'};
       // List<String> header= new List<String> {'AssigneeId','PS Status' , 'Permissionset Name', 'division'};
        String csvColumnHeader = String.join(header,',') ;
        List<String> csvRowValues = new List<String>();
        csvRowValues.add(csvColumnHeader);
        for(UserDetail currUser : theListReturnedFromMethod2){
            String Id = currUser.Id != null ? String.valueOf(currUser.Id).escapeCsv() : ''; 
           // String status = currUser.status != null ? String.valueOf(currUser.status).escapeCsv() : '';
            String FirstName = currUser.FirstName != null ? String.valueOf(currUser.FirstName).escapeCsv() : '';
            String LastName = currUser.LastName != null ? String.valueOf(currUser.LastName).escapeCsv() : '';
            String Username = currUser.Username != null ? String.valueOf(currUser.Username).escapeCsv() : '';
            String Email = currUser.Email != null ? String.valueOf(currUser.Email).escapeCsv() : '';
            String Division = currUser.Division != null ? String.valueOf(currUser.Division).escapeCsv() : '';
            system.debug('divisoncsv'+Division);
            String Department = currUser.Department != null ? String.valueOf(currUser.Department).escapeCsv() : '';
          
            String status = currUser.status != null ? String.valueOf(currUser.status).escapeCsv() : '';
            String permissionSetList = currUser.permissionSetList != null ?   String.join(currUser.permissionSetList,'\n').escapeCSV():'';
            String rowStr = id + ',' + status + ',' + permissionSetList + ','+ Division   ;
            csvRowValues.add(rowStr);
        }
        String csvname='Persona Report'+'.csv';
        String VersionData= String.join(csvRowValues,'\n');
        ContentVersion cv = new ContentVersion();
        cv.ContentLocation = 'S'; 
        cv.Title = csvname; 
        cv.PathOnClient = csvname; 
        cv.VersionData = Blob.valueOf(versionData); 
        insert cv;
        system.debug('personId'+ cv.id);
        
        
        
    }
    
}
Prateek Prasoon 25Prateek Prasoon 25
Answer :-
Based on the provided code, it seems that the issue is with the variable naming in the UserDetail class. In the evaluateUser method, you are trying to assign the Division value from the permissionSetMap to the UserDetail object, but the variable name is misspelled as Ud.Division instead of ud.Division. Therefore, it is throwing an error as the Division variable is not defined in the Ud object.
To fix this error, you can change the following line of code in the evaluateUser method:
 
Text
      
    
    
    
      Ud.Division=permissionSetMap.get(id).division;

to:-
Text
      
    
    
    
      ud.Division=psa.Assignee.Division;

This will assign the Division value from the PermissionSetAssignment object to the UserDetail object.
Similarly, you can add other fields such as FirstName, LastName, Username, and Email by assigning the corresponding values from the PermissionSetAssignment object to the UserDetail object.
For example:
Text
      
    
    
    
      ud.FirstName = psa.Assignee.FirstName;
ud.LastName = psa.Assignee.LastName;
ud.Username = psa.Assignee.Username;
ud.Email = psa.Assignee.Email;

If you find my answer helpful, please mark it as the best answer. Thanks!