Codementor Events

How to find users with a Specific User License in Salesforce?

Published Jul 15, 2024

Although we can find the number of licenses available and consumed in the Company Information section of the Settings in Salesforce. But some time we need to toggle between the users to provide user license to new users and remove the existing ones in that case the below kind of solution works to quickly find who has got what.

Here is one quick note on how to find the user licenses that are associated to users.

Map<Id,Profile> profileIds = new Map<id,profile>([SELECT Id,UserLicenseId FROM Profile where UserLicenseId in (SELECT Id FROM UserLicense where Name ='Salesforce')]);

List<User> users = [select id,username,Profile.name from user where profileId in:profileIds.Keyset()];

//Print the usersname along with their profile name
for(User us : users){
  System.debug(us.username + ' ' + us.Profile.Name);    
}

Another way of finding the result in single query is given below and also it is more efficient

List<User> usersWithUserLicenseList = [
    Select Id, Name, Profile.UserLicense.Name 
    From User 
    Where Profile.UserLicense.Name = 'Salesforce'
];

Peace ✌️

Discover and read more posts from Sunil Kumar
get started