Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I want query the list of user with salesforce license

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

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
1  
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' –  Peter Knolle Jan 3 at 9:20
    
How about you turn that into an answer so that I can give a true upvote ? :-) –  Samuel De Rycke Jan 3 at 9:22
    
Thank you . I am working on it –  Teja Jan 3 at 9:23
    
@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. :) –  Peter Knolle Jan 3 at 9:59
1  
It's a different, more elegant approach. In my opinion that's worth an answer of it's own. –  Samuel De Rycke Jan 3 at 10:03
show 1 more comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.