How to query all user with salesforce license?

I want query the list of user with salesforce license

List<user> user11=[SELECT name from user limit 5000];

Answer

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.

Attribution
Source : Link , Question Author : Teja , Answer Author : Community

Leave a Comment