I need to get users list which is group by the Salesforce licence. I need to get Permission set licences as well. Is there any way to do this?
1 Answer
You can collect the users based on the License type, please refer the below code to collect a map of LicenseId to List of Users (can collect based on License Name as well).
Map<String, List<User>> mapLicenseToLstUser = new Map<String, List<User>>();
Map<Id, List<User>> mapLicenseIdToLstUser = new Map<Id, List<User>>();
for(User objUser : [select Id, Profile.UserLicense.Name From User Where Profile.UserLicenseId != null]) {
if(!mapLicenseToLstUser.containsKey(objUser.Profile.UserLicense.Name)) {
mapLicenseToLstUser.put(objUser.Profile.UserLicense.Name, new List<User>());
}
mapLicenseToLstUser.get(objUser.Profile.UserLicense.Name).add(objUser);
if(!mapLicenseIdToLstUser.containsKey(objUser.Profile.UserLicenseId)) {
mapLicenseIdToLstUser.put(objUser.Profile.UserLicenseId, new List<User>());
}
mapLicenseIdToLstUser.get(objUser.Profile.UserLicenseId).add(objUser);
}
system.debug('=======mapLicenseToLstUser======'+mapLicenseToLstUser);
system.debug('=======mapLicenseIdToLstUser======'+mapLicenseIdToLstUser);
PermissionSetLicenseAssign
andPermissionSetLicense
sobjects