13

I want query the list of user with salesforce license

List<user> user11=[SELECT name from user limit 5000];
4
  • 2
    If this is not related to the visualforce tag, please edit your question to remove it. Else, some elaboration may be meaningful. Jan 3, 2014 at 9:07
  • Any visualforce user can also try this one that why i wrote this tag
    – Teja
    Jan 3, 2014 at 9:10
  • i need to filter this query with salesforce license
    – Teja
    Jan 3, 2014 at 9:10
  • 1
    But your question is not about visualforce, tags are meant to categorize questions. Jan 3, 2014 at 9:13

6 Answers 6

18

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.

5
  • 5
    You can specify the relationship fields in the Where clause, e.g., Select Id, Name, Profile.UserLicense.LicenseDefinitionKey From User Where Profile.UserLicense.LicenseDefinitionKey = 'SFDC' Jan 3, 2014 at 9:20
  • 1
    How about you turn that into an answer so that I can give a true upvote ? :-) Jan 3, 2014 at 9:22
  • 1
    @Sdry - Sometimes I struggle with when to answer or comment. I really just looked at your code and thought of something versus thinking it out on my own, so I commented. :) Jan 3, 2014 at 9:59
  • 2
    It's a different, more elegant approach. In my opinion that's worth an answer of it's own. Jan 3, 2014 at 10:03
  • @Sdry - Alright. You convinced me. :-) Jan 4, 2014 at 17:34
35

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.

1

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.

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_user.htm

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.

0

From the Dev console (query editor):

Select Id, Name, IsActive, Profile.UserLicense.Name 
From User 
Where Profile.UserLicense.Name = 'Salesforce' 
  AND IsActive=True
1
  • 3
    Welcome to Salesforce Stack Exchange (SFSE), Simone! Readers please note that this is the same query as the second one in Peter's answer, but with the check for isActive added. While this additional check could be quite useful for some, please read Peter's thoughts on the difference between his first and second queries.
    – Moonpie
    Jan 13, 2022 at 12:33
-2
    map<id, id> profileIdVSuserId = new map<id, id>();
    for(User u : [SELECT ProfileId, id FROM User]){
        profileIdVSuserId.put(u.ProfileId, u.Id);
    }
    
    map<id, string> userLicIdVSuserLicName = new map<id, string>();
    for(UserLicense ul : [SELECT id, Name FROM UserLicense]){
        userLicIdVSuserLicName.put(ul.Id, ul.Name);
    }
    
    map<id, string> userIdVSuserLicName = new map<id, string>();
    for(Profile p1 : [SELECT id, UserLicenseId FROM Profile]){
        if(profileIdVSuserId.containsKey(p1.id) && userLicIdVSuserLicName.containsKey(p1.UserLicenseId)){
           userIdVSuserLicName.put(profileIdVSuserId.get(p1.id), userLicIdVSuserLicName.get(p1.UserLicenseId)); 
        }
    }
    
    system.debug('userIdVSuserLicName ===> ' + userIdVSuserLicName);
-3
Map<Id,Profile> usermap = new Map<id,profile>([SELECT Id,UserLicenseId FROM Profile where name ='Salesforce']);

List<user> standardProfileUsers = [select id from user where profileId in:usermap.Keyset()];
1
  • 2
    That will not get any users without a Profile Named Salesforce and does not answer the OP's question.
    – Eric
    Jul 29, 2015 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .