I want query the list of user with salesforce license
List<user> user11=[SELECT name from user limit 5000];
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
Didn't find how to do it in a single query, but this gives all users with a Salesforce user licence type. Important to know is that you have to query this through the profile.
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> standardProfileUsers = [select id from user where profileId in:profileIds.Keyset()];
Update: Peter's answer is better: He shows that you can infact get these results in a single query, which is prefered for performance and cleaner in code.
Take a look at the UserLicense object documentation. There is a field called
LicenseDefinitionKey that uniquely identifies a particular user license and the documentation lists all possible values. From the documentation:
A string that uniquely identifies a particular user license. Label is License Def. ID. Values are:
And one of the values:
SFDC: corresponds to the Full CRM user license
If what you want is the Users with the Full CRM license than the following query can get them:
List<User> usersWithSfdcLicense = [ Select Id, Name, Profile.UserLicense.LicenseDefinitionKey From User Where Profile.UserLicense.LicenseDefinitionKey = 'SFDC' ];
If what you want is the Users with a License Name of Salesforce than the following query can get them:
List<User> usersWithSalesforceLicense = [ Select Id, Name, Profile.UserLicense.Name From User Where Profile.UserLicense.Name = 'Salesforce' ];
Those two queries return the same Users in my org. I suppose it's possible that the UserLicense.Name field could be changed at some point by Salesforce to be something else for the Full CRM License, whereas the UserLicense.LicenseDefinitionKey field is documented as the way to identify a User License.
Although I have used Peter's answer in the past, I stumbled across the following which was a bit simpler:
SELECT id, Name FROM User WHERE usertype IN ('Standard')
This does however include Salesforce Platform license users (but not chatter free). See this link for the other options.
This fit my use case perfectly as I needed to include all users with a Salesforce license, which included those with Salesforce portal licenses in my situation.
Please note that the question here is "How to query all users with a salesforce license" - but which types of licenses is not specified - so I believe this answer is valid.